How should I organize my files (controllers, models, helpers, etc.) in folders using Xcode and what's the best way of accessing them?
Follow the MVC Structure Like this below images
Aft er opening file structure, project should be like below
Home structure, containing all VC's that are in HomeStoryboard(here, main storyboard)
NOTE: Same structure must be followed in physical directory.
Reference: https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
Edit
File organisation is a fundamental method to keep you files in folder like there are books on shelf,I mean same subject book on same row type.
Accessing file doesn't matter on your file hierarchy. If file is added in your project directory, then x-code automatically recognise your files and suggest you accordingly.
for every file you have to make init method to make that file into memory, or you can override init method.
Take ref : https://medium.com/ios-objective-creation/lesson-2-creating-custom-classes-in-objective-c-17f760ce9732
Now I am doing with this new pattern and this helped me alot.
Now you do not need MVC folder, I mean add Model , View , Controller in parent directory and remove MVC dir. Check below
Here is the brief explanation of every topic.
Here below, Device.swift file have device related functions like Is_Ipad , is_iPhone6_7_8 etc.
CallAPI+Home
is the extension of APIs for the Home module. Every module have separate extensions. Its working is described in last.
If your App is small then you can skip this part and directly use API's from Utility.
Flow Diagram
1. First controller calls CallAPI, every API have its own func to get data and bind into it model.
2. CallAPI calls Webservices and decides type of API (post, get).
3. WebServices have alomofire call service. It gets data from service and return back to Call API.
4. If API result success, then CallAPI binds model data else it shows error alert.
5. Now Model data is returned to the Call API.
6. Now callAPi returns Model data to controller.
Some sample code
In controller,
func validateUserAPI(){
guard self.isValidateTF() else {
return
}
let param = [ "sPassword": Common.encodeBase64(withOutPass: tfPassword.text!)!,
"sUserName": Common.encodeBase64(withOutPass: tfUsername.text!)!
] as [String : Any]
// Validate User
CallAPI.submitValidateUser(params: param) { [weak self] (submitResult) in
guard let aSelf = self else {return}
let aResultValidate = submitResult.validateUserResult[0]
if aResultValidate.status == 0 {
Alert.showAlertError(title: "Error", body: aResultValidate.message)
}
else {
// save data to user defaults
}
}
}
In CallAPI, I have created success handling only, if failure handler is required then create it too.
extension CallAPI {
class func submitValidateUser( params: [String : Any], completionHandler: @escaping ((LoginValidateUser) -> Void)) {
HUD.startIndeterminate("Loading...")
WebService.postAlamofire(API.ValidateUser, params: params) { (errMsg, result) in
HUD.stopIndeterminate()
if result.isSuccess {
// If data success, bind data into model and return back to controller
let resultModel = LoginValidateUser(fromDictionary: result.value! as! [String : Any])
completionHandler(resultModel)
}
else {
Alert.showAlertError(title: "Error", body: errMsg!)
}
}
}
}
In web-services,
class WebService {
class func getAlamofire(_ url : String, completionHandler: @escaping (( String?, Result<Any>) -> Void) ){
Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (jsonResult) in
completionHandler((jsonResult.error?.localizedDescription),jsonResult.result)
}
}
class func postAlamofire(_ url : String, params: [String:Any], completionHandler: @escaping (( String?, Result<Any>) -> Void) ){
print("************************************")
print("************************************")
print("URL ----> \(url)")
print("INPUT ----> \(params)")
Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON { (jsonResult) in
print("OUTPUT ---->")
print(jsonResult.result.value)
completionHandler((jsonResult.error?.localizedDescription),jsonResult.result)
}
}
}
In helper I created struct, here one struct is for API lists.
struct API {
static let ValidateUser = "\(kBaseURL)ValidateUser"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With