Если вам нужно сообщить N экземплярам об ответе API, вы можете использовать NotificationCenter
или KVO
, а если вам нужен только доступ к результату API через приложение, то вы можете используйте Singleton (если данные временно требуются) или Постоянное хранилище (если данные должны храниться постоянно), например NSUserDefaults
, SQLite
или CoreData
Пример @Moshe Gottlieb
// API Class
class MyClass{
static let myNotification = Notification.Name("com.mycompany.intersting-notification")
// API Call
func apiCall(){
// on API completion, post(publish or send) a notification using the syntax
NotificationCenter.default.post(name: MyClass.myNotification, object: "Hello!")
}
}
// UIViewController / Any class initial load method
override func viewDidLoad() {
super.viewDidLoad()
// register or observe or listen for notification of name
NotificationCenter.default.addObserver(forName: MyClass.myNotification, object: nil, queue: nil) { (notification) in
// closure called when notification is received
if let str = notification.object as? String { // data fetched from notification
print("Somebody sent us this: \(str)!")
}
}
}