0

I coding an app with several (15-25 different swigft files one for each view. Some variables and functions I will use in every viewcontroller. What would be best practice to enable code reusage?

For instance I need to communicate with a server in which the first request is for an access token, this request I imagine could be a global function setting a global variable (access token). And then using it for the more specific requests.

I started placing a lot of global constants in appdelegate file, in a Struct is there a problem with this?

LibraryAPI.swift import UIKit import CoreData

class LibraryAPI: NSObject
{
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
private var loginD: LoginDetails
private var isOnline: Bool

class var sharedInstance: LibraryAPI
{
    struct Singleton
    {
        static let instance = LibraryAPI()
    }
    return Singleton.instance
}
override init()
{
    super.init()
}
func getIsOnline() -> Bool
{
    return isOnline
}
func setIsOnline(onlineStatus: Bool)
{
    isOnline = onlineStatus
}
func getLoginDetails() -> LoginDetails
{
    return loginD
}
func setLoginDetails(logindet: LoginDetails)
{
    loginD = logindet
}

// Execute the fetch request, and cast the results to an array of objects
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [LoginDetails] {
   setLoginDetails(fetchResults[0])  
}

}

Claus Machholdt
  • 187
  • 3
  • 12
  • There is no one answer to this. The example of an access token could be very appropriate to store in the keychain instead of a variable, or it could be a local variable in a data controller that handles your api, etc. – Peter DeWeese Feb 27 '15 at 13:35

2 Answers2

1

You should avoid using global variables.

Depending on what you have / what you need to do, either you can :

  • Create a class and make an instance on your first call. Then, you can pass the object through your views (prepareForSegue). But that can still be repetitive to implement everytime ;
  • Create a singleton class that will be instantiate only once and accessible from everywhere (singleton are considered as a bad practice by some);
  • Use the NSUserDefaults to store String ;
  • Save your data somehow (CoreData, ...).
lchamp
  • 6,592
  • 2
  • 19
  • 27
  • Do yo have an example of a singleton application? I'm especially in doubt how i can establish connection and use core data. – Claus Machholdt Mar 01 '15 at 15:22
  • Here is a singleton I use in order to play sound : http://pastebin.com/JPzaLhZd (sorry for syntax colorization). To use it, you would just do : `SoundPlayer.sharedInstance.playSound()` for example (of course you can also access properties not only methods). – lchamp Mar 01 '15 at 15:28
  • This is killing me :-) Would you please take a look at the updated code in the question. I have no problem retrieving the properties from the LibraryAPI class. But where and how should i connect the variables/properties to their counterparts which are in the Core Data? Please give example if you have. – Claus Machholdt Mar 01 '15 at 19:42
0

You can do like this

User.swift

import Foundation
import UIKit

class User: NSObject {
    var name: String = ""

    func getName() -> String{
        name = "Nurdin"
        return name
    }

}

ViewController.swift

import Foundation
import UIKit

let instanceOfUser = User()
println(instanceOfUser.getName()) // return Nurdin
Nurdin
  • 23,382
  • 43
  • 130
  • 308