DEV Community

Discussion on: Explain iOS development like I'm five

Collapse
 
rapidnerd profile image
George

I've done a little bit of swift in before and can answer a handful of the questions.

Reusable functions

Swift allows methods to be static meaning you can call them without having to call a new instance of the class, similar to Java. I tend to make a class dedicated to them if needed for example:

class Helper {
  static func postRequest() -> [String:String] {
    return ["something here" : "something also here"]
 }
}
Enter fullscreen mode Exit fullscreen mode

Can then be called like so

Helper.postRequest()
Enter fullscreen mode Exit fullscreen mode

Additionally i found it's possible to use structs as well as extensions

struct BackendError {
    var message : String
}

struct SuccessCall {
    var json : JSON

    var containsError : Bool {
        if let error = json["error"].string {
            return true
        }
        else {
            return false
        }

    }
}

typealias FailureBlock  = (BackendError) -> Void
typealias SuccessBlock  = (SuccessCall) -> Void

typealias AlamoFireRequest = (path: String, method: Alamofire.Method, data: [String:String]) -> Request
typealias GetFunction = (path: String , data: [String : String], failureBlock: FailureBlock, successBlock: SuccessBlock) -> Void

class Backend {
   func getRequestToBackend (token: String )(path: String , data: [String : String], failureBlock: FailureBlock, successBlock: 

}
Enter fullscreen mode Exit fullscreen mode

Extension example


extension Array {
    func sampleItem() -> T {
        let index = Int(arc4random_uniform(UInt32(self.count)))
        return self[index]
    }
}
Enter fullscreen mode Exit fullscreen mode

AppDelegate

It kind of acts like a template of basic code needed to run the project out of the box. From this example it shows that its using the UIKit framework, and by seeing this we can see that the class needs to have some form of connection to the user interface

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
}
Enter fullscreen mode Exit fullscreen mode

The @UIApplicationMain attribute indicates that the application delegate is using this function and passing the class's name as the name of the delegate class.

Ways

I think that with swift there is multiple right ways, with the little experience I found that multiple methods and ways of doing something can always work as intended.

I believe majority of this info is correct, my swift knowledge is pretty low. But hope it can help in some way :p

Collapse
 
ben profile image
Ben Halpern

Great!

Collapse
 
furkanvijapura profile image
FURKAN VIJAPURA

Superb man