Using XCode 8 beta 6 under El Capitan coding in Swift 3.0
Trying to translate these lines in a project from Swift 2.0 to Swift 3.0
let userInfo = ["peer": peerID, "state": state.toRaw()]
NSNotificationCenter.defaultCenter.postNotificationName("Blah", object: nil, userInfo: userInfo)
So I managed to cobble together this ...
public class MyClass {
static let myNotification = Notification.Name("Blah")
}
let userInfo = ["peerID":peerID,"state":state.rawValue] as [String : Any]
NotificationCenter.default.post(name: MyClass.myNotification, object: userInfo)
It compiles and sends the notification when I run it and setup a listener with this line, but with no userInfo that I can decode?
let notificationName = Notification.Name("Blah")
NotificationCenter.default.addObserver(self, selector: #selector(peerChangedStateWithNotification), name: notificationName, object: nil)
This code prints "nil" as in no userInfo ...
func peerChangedStateWithNotification(notification:NSNotification) {
print("\(notification.userInfo)")
}
As #vadian said, NotificationCenter has a
post(name:object:userInfo:) method which you can use.
Here is a self-contained example, which also demonstrates how
to convert the userInfo back to a dictionary of the expected type
(taken from https://forums.developer.apple.com/thread/61578):
class MyClass: NSObject {
static let myNotification = Notification.Name("Blah")
override init() {
super.init()
// Add observer:
NotificationCenter.default.addObserver(self,
selector: #selector(notificationCallback),
name: MyClass.myNotification,
object: nil)
// Post notification:
let userInfo = ["foo": 1, "bar": "baz"] as [String: Any]
NotificationCenter.default.post(name: MyClass.myNotification,
object: nil,
userInfo: userInfo)
}
func notificationCallback(notification: Notification) {
if let userInfo = notification.userInfo as? [String: Any] {
print(userInfo)
}
}
}
let obj = MyClass()
// ["bar": baz, "foo": 1]
Alternatively, you can extract the dictionary values in the
callback like this (also from above Apple Developer Forum thread):
func notificationCallback(notification: Notification) {
guard let userInfo = notification.userInfo else { return }
if let fooValue = userInfo["foo"] as? Int {
print("foo =", fooValue)
}
if let barValue = userInfo["bar"] as? String {
print("bar =", barValue)
}
}
Related
I am trying to implement a singleton class with AVPlayer in it. The key value observing throws exception. I think the object of this class is getting dealloced.
import Foundation
import UIKit
import AVKit
class Player: NSObject, ObservableObject {
var player : AVPlayer!
var playerController = AVPlayerViewController()
var presentingVC : UIViewController!
static let shared = Player()
private override init() { }
func play(urlString: String) {
let auth = Authentication()
let headers: [String: String] = ["x-auth-token" : auth.token]
let url = URL(string: urlString)
let asset = AVURLAsset(url: url!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
let playerItem = AVPlayerItem(asset: asset)
//let avPlayer = AVPlayer(url: url!)
player = AVPlayer(playerItem: playerItem)
player.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.new, .initial], context: nil)
player.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem.status), options: [.new, .initial], context: nil)
let center = NotificationCenter()
center.addObserver(self, selector: Selector(("newErrorLogEntry")), name: .AVPlayerItemNewErrorLogEntry, object: player.currentItem)
center.addObserver(self, selector: Selector(("failedToPlayTillEnd")), name: .AVPlayerItemFailedToPlayToEndTime, object: player.currentItem)
playerController.player = player
presentingVC.present(playerController, animated: true) {
self.player.play()
}
}
func newErrorLogEntry(_ notification: Notification) {
guard let object = notification.object, let playerItem = object as? AVPlayerItem else {
return
}
guard let errorLog: AVPlayerItemErrorLog = playerItem.errorLog() else {
return
}
print("2")
NSLog("Error: \(errorLog)")
}
func failedToPlayToEndTime(_ notification: Notification) {
let error = notification.userInfo!["AVPlayerItemFailedToPlayToEndTimeErrorKey"]
print("3")
NSLog("Error: \(String(describing: error))")
DispatchQueue.main.async {
let alert = UIAlertController(title: "Playback error {}{}", message: "Unable to Play Channel {}{} \n", preferredStyle: UIAlertController.Style.alert)
self.playerController.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: { (UIAlertAction) in
self.playerController.dismiss(animated: true, completion: nil)
print("Cancel")
}))
}
}
}
Calling from ViewController class
Player.shared.presentingVC = self
Player.shared.play(urlString: url)
Following is the exception:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<VideoPlayer_v2.Player: 0x600001f84cc0>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: status
Observed object: <AVPlayer: 0x600001dc1550>
Change: {
kind = 1;
new = 0;
}
Looks like AVPlayer.status message is received. But the object is already de-allocated. Correct me if I am wrong.
What is the best way to separate out AVPlayer functions in a separate class other than UIViewController class?
The reason for the exception is that you are adding observers inside your Player class, but you are not observing the values. To do that, add this method in side Player class then you handle the observed values as you need:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if object as AnyObject? === player {
if keyPath == "status" {
// Do something like this or whatever you need to do:
if player.status == .readyToPlay {
player.play()
}
} else if keyPath == "currentItem.status" {
// Do something
} else {
// Do something
}
}
}
After upgrading to iOS10 users started complaining about crashes of my app.
I am testing it with iOS10 on the simulator and indeed the app crashes with a message saying "Could not cast value of type '__NSArrayI' to 'NSMutableArray'". Here's my code, please help:
import Foundation
protocol getAllListsModel: class {
func listsDownloadingComplete(downloadedLists: [ContactsList])
}
class ListsDownloader: NSObject, NSURLSessionDataDelegate{
//properties
weak var delegate: getAllListsModel!
var data : NSMutableData = NSMutableData()
func downloadLists() {
let urlPath: String = "http://..."
let url: NSURL = NSURL(string: urlPath)!
var session: NSURLSession!
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration() //defaultSessionConfiguration()
session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTaskWithURL(url)
task.resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
self.data.appendData(data);
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error != nil {
print("Failed to download data")
}else {
self.parseJSON()
print("Lists downloaded")
}
}
func parseJSON() {
var jsonResult: NSMutableArray = NSMutableArray()
do{
try jsonResult = NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray
} catch let error as NSError {
print(error)
}
var jsonElement: NSDictionary = NSDictionary()
var downloadedLists: [ContactsList] = []
for i in 0...jsonResult.count-1 {
jsonElement = jsonResult[i] as! NSDictionary
let tempContactsList = ContactsList()
//the following insures none of the JsonElement values are nil through optional binding
let id = jsonElement["id"] as? String
let name = jsonElement["name"] as? String
let pin = jsonElement["pin"] as? String
let lastUpdated = jsonElement["created"] as? String
let listAdminDeviceID = jsonElement["admin"] as? String
tempContactsList.id = id
tempContactsList.name = name
tempContactsList.pin = pin
tempContactsList.lastUpdated = lastUpdated
tempContactsList.listAdmin = listAdminDeviceID
downloadedLists.append(tempContactsList)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate.listsDownloadingComplete(downloadedLists)
})
}
}
Even in iOS 9, there was no guarantee NSJSONSerialization.JSONObjectWithData(_:options:) would return mutable object or not. You should have specified NSJSONReadingOptions.MutableContainers.
And in your code, you are not modifying jsonResult, which means you have no need to declare it as NSMutableArray. Just replace NSMutableArray to NSArray, and then you have no need to specify NSJSONReadingOptions.MutableContainers.
But as vadian is suggesting, you better use Swift types rather than NSArray or NSDictionary. This code should work both in iOS 9 and 10.
func parseJSON() {
var jsonResult: [[String: AnyObject]] = [] //<- use Swift type
do{
try jsonResult = NSJSONSerialization.JSONObjectWithData(self.data, options: []) as! [[String: AnyObject]] //<- convert to Swift type, no need to specify options
} catch let error as NSError {
print(error)
}
var downloadedLists: [ContactsList] = []
for jsonElement in jsonResult { //<- your for-in usage can be simplified
let tempContactsList = ContactsList()
//the following insures none of the JsonElement values are nil through optional binding
let id = jsonElement["id"] as? String
let name = jsonElement["name"] as? String
let pin = jsonElement["pin"] as? String
let lastUpdated = jsonElement["created"] as? String
let listAdminDeviceID = jsonElement["admin"] as? String
tempContactsList.id = id
tempContactsList.name = name
tempContactsList.pin = pin
tempContactsList.lastUpdated = lastUpdated
tempContactsList.listAdmin = listAdminDeviceID
downloadedLists.append(tempContactsList)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate.listsDownloadingComplete(downloadedLists)
})
}
Try this and check it on iOS 10 devices.
(The as! conversion would cause some weird crashes when your server is malfunctioning, but that would be another issue, so I keep it there.)
I'm trying to do a basic unit test with the keychain. I can set the value for a key successfully ('setString' returns true), however, when I try to retrieve the value, even after a 5 second delay, the return is still nil:
class MyKeychainTest: XCTestCase {
func checkKeychain(timer: NSTimer) {
debugPrint("check keychain...")
let userInfo = timer.userInfo as! [String: AnyObject]
let expectation = userInfo["expectation"] as! XCTestExpectation
let res = KeychainWrapper.objectForKey("myKey")
debugPrint("got res: \(res)")
XCTAssertNotNil(res)
expectation.fulfill()
}
func testKeychain() {
let expectation = expectationWithDescription("gotKey")
let success = KeychainWrapper.setString("foo", forKey: "myKey")
debugPrint("set key?: \(success)")
NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(self.checkKeychain)
, userInfo: ["expectation": expectation ], repeats: false)
self.waitForExpectationsWithTimeout(10, handler: nil)
}
}
Any ideas on what can cause this?
Thanks
Accidentally used the wrong method to get the key. Instead of KeychainWrapper.objectForKey("myKey") using KeychainWrapper.stringForKey("myKey") works. Strange that an object isn't even returned though in the former case
The book states,
“An ensemble identifier is used to match stores across devices. It is
important that this be the same for each store in the ensemble.”
let ensembleFileSystem = CDEICloudFileSystem(ubiquityContainerIdentifier: "???")
Does this need to be unique for all users ? or just for my application?
If anyone has a Swift version of how the set up Ensembles that would be great.
What I have so far, is this all that is needed?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let ensembleFileSystem = CDEICloudFileSystem(ubiquityContainerIdentifier: "???")
let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension: "momd")!
let url = applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
let ensemble = CDEPersistentStoreEnsemble(ensembleIdentifier: "mainstore", persistentStoreURL: url, managedObjectModelURL: modelURL, cloudFileSystem: ensembleFileSystem!)
if !ensemble.leeched {
ensemble.leechPersistentStoreWithCompletion { (error) -> Void in
if error != nil {
print("cannot leech")
print(error!.localizedDescription)
}
}
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "syncWithCompletion:", name: CDEMonitoredManagedObjectContextDidSaveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "syncWithCompletion:", name: CDEICloudFileSystemDidDownloadFilesNotification, object: nil)
return true
}
func syncWithCompletion(notification:NSNotification) {
print("synced \(notification)")
managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
}
Something is missing Im getting this error log
User is not logged into iCloud
Despite being logged in as evident
print(NSFileManager.defaultManager().ubiquityIdentityToken)
Not being nil
Got it to work in the end - found example apps in 1.0 Git
I belive I was leeching to fast - not giving enough time for the set up process to complete.
Support this framework - buy ensembles 2, if you like ver 1.
Update .. easier way
I just use the normal core data stack apple provides.
Here is the extras to get ensembles working.
var ensemble:CDEPersistentStoreEnsemble!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let file = CDEICloudFileSystem(ubiquityContainerIdentifier: nil)
let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension: "momd")!
let storeurl = self.applicationDocumentsDirectory.URLByAppendingPathComponent("store.sqlite")
ensemble = CDEPersistentStoreEnsemble(ensembleIdentifier: "MyStoreName", persistentStoreURL: storeurl, managedObjectModelURL: modelURL, cloudFileSystem: file)
ensemble.delegate = self
NSNotificationCenter.defaultCenter().addObserver(self, selector: "localSaveOccurred:", name: CDEMonitoredManagedObjectContextDidSaveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "cloudDataDidDownload:", name: CDEICloudFileSystemDidDownloadFilesNotification, object: nil)
syncWithCompletion { completed in
if completed {
print("SUCCESSS")
}
else {
print("FAIL")
}
}
return true
}
// MARK: - Sync
func applicationDidEnterBackground(application: UIApplication) {
print("Did Enter Background Save from App Delegate")
let identifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler(nil)
saveContext()
syncWithCompletion { (completed) -> Void in
if completed {
UIApplication.sharedApplication().endBackgroundTask(identifier)
}
}
}
func applicationWillEnterForeground(application: UIApplication) {
syncWithCompletion { (completed) -> Void in
}
}
func localSaveOccurred(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
}
}
func cloudDataDidDownload(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
print("items from iCloud arrived")
}
}
func syncWithCompletion(completion:(completed:Bool) -> Void) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
if !ensemble.leeched {
ensemble.leechPersistentStoreWithCompletion(nil)
}
else {
ensemble.mergeWithCompletion{ error in
if error != nil {
print("cannot merge \(error!.localizedDescription)")
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
completion(completed: false)
}
else {
print("merged")
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
completion(completed: true)
}
}
}
}
// MARK: - Ensemble Delegate Methods
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, didSaveMergeChangesWithNotification notification: NSNotification!) {
managedObjectContext.performBlockAndWait { () -> Void in
self.managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
}
}
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, globalIdentifiersForManagedObjects objects: [AnyObject]!) -> [AnyObject]! {
return (objects as NSArray).valueForKeyPath("uniqueIdentifier") as! [AnyObject]
}
My First Way
Here it is in Swift, with a few extras
var ensemble:CDEPersistentStoreEnsemble!
var cloudFileSystem:CDEICloudFileSystem!
var managedObjectContext: NSManagedObjectContext!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
setUpCoreData()
let modelURL = NSBundle.mainBundle().URLForResource("YourDataModel", withExtension: "momd")!
cloudFileSystem = CDEICloudFileSystem(ubiquityContainerIdentifier:"USE_YOUR_APPS_REVERSE DOMAIN NAME HERE")
From the developer: RE ubiquityContainerIdentifier
This is not part of Ensembles per se. It is from iCloud. Every app
using iCloud has to have a ubiquity container id. You can find it in
your app settings when you enable iCloud. It is unique per app, and we
only use it if you are choosing for iCloud (eg not Dropbox).
ensemble = CDEPersistentStoreEnsemble(ensembleIdentifier: "store", persistentStoreURL: storeURL(), managedObjectModelURL: modelURL, cloudFileSystem: cloudFileSystem!)
ensemble.delegate = self
NSNotificationCenter.defaultCenter().addObserver(self, selector: "localSaveOccurred:", name: CDEMonitoredManagedObjectContextDidSaveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "cloudDataDidDownload:", name: CDEICloudFileSystemDidDownloadFilesNotification, object: nil)
syncWithCompletion { completed in
if completed {
print("SUCCESSS")
}
else {
print("FAIL")
}
}
return true
}
// MARK: - Core Data Stack
func setUpCoreData() {
let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension: "momd")!
guard let model = NSManagedObjectModel(contentsOfURL: modelURL) else { fatalError("cannot use model") }
do {
try NSFileManager.defaultManager().createDirectoryAtURL(storeDirectoryURL(), withIntermediateDirectories: true, attributes: nil)
}
catch {
fatalError("cannot create dir")
}
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
//NSDictionary *options = #{NSMigratePersistentStoresAutomaticallyOption: #YES, NSInferMappingModelAutomaticallyOption: #YES};
let failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL(), options: nil)
managedObjectContext = NSManagedObjectContext.init(concurrencyType: .MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
abort()
}
}
func storeDirectoryURL() -> NSURL {
let directoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
return directoryURL
}
func storeURL() -> NSURL {
let url = storeDirectoryURL().URLByAppendingPathComponent("store.sqlite")
return url
}
// MARK: - Sync
func applicationDidEnterBackground(application: UIApplication) {
print("Did Enter Background Save from App Delegate")
let identifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler(nil)
saveContext()
syncWithCompletion { (completed) -> Void in
if completed {
UIApplication.sharedApplication().endBackgroundTask(identifier)
}
}
}
func applicationWillEnterForeground(application: UIApplication) {
syncWithCompletion { (completed) -> Void in
}
}
func localSaveOccurred(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
}
}
func cloudDataDidDownload(note:NSNotification) {
syncWithCompletion { (completed) -> Void in
}
}
func syncWithCompletion(completion:(completed:Bool) -> Void) {
if !ensemble.leeched {
ensemble.leechPersistentStoreWithCompletion { error in
if error != nil {
print("cannot leech \(error!.localizedDescription)")
completion(completed: false)
}
else {
print("leached!!")
completion(completed: true)
}
}
}
else {
ensemble.mergeWithCompletion{ error in
if error != nil {
print("cannot merge \(error!.localizedDescription)")
completion(completed: false)
}
else {
print("merged!!")
completion(completed: true)
}
}
}
}
// MARK: - Ensemble Delegate Methods
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, didSaveMergeChangesWithNotification notification: NSNotification!) {
print("did merge changes with note")
managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
}
func persistentStoreEnsemble(ensemble: CDEPersistentStoreEnsemble!, globalIdentifiersForManagedObjects objects: [AnyObject]!) -> [AnyObject]! {
return (objects as NSArray).valueForKeyPath("uniqueIdentifier") as! [AnyObject]
}
Code:
AppDelegate.swift
func application(application:UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!)
{
let entityDescription =
NSEntityDescription.entityForName("Quote",
inManagedObjectContext: managedObjectContext!)
let request = NSFetchRequest()
request.entity = entityDescription
let pred = NSPredicate(format: "(quoteDate = %#)", "2015-03-08")
request.predicate = pred
var error: NSError?
var objects = managedObjectContext?.executeFetchRequest(request,
error: &error)
if let results = objects {
if results.count > 0 {
arrQuotes = NSMutableArray()
for(var i=0;i<results.count;i++){
let match = results[i] as! NSManagedObject
var quote = match.valueForKey("quote") as! NSString
arrQuotes.addObject(quote)
}
var dict = ["test": arrQuotes]
reply(dict)
} else {
}
}
catecontroller.swift
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
arrQuotes = NSMutableArray()
var dict = ["test" : arrQuotes]
if !WKInterfaceController.openParentApplication(dict, reply: { (reply,error) -> Void in
println("\(reply)") //your reply data as Dictionary
self.arrQuotes = dict["test"]!
println("\(self.arrQuotes.count)")
}) {
println("ERROR")
}
I am doing a sample watchkit project.What i am trying to do is fetch data from iPhone side and send it to watchkit app.I try to return the value as array of dictionary.but in watchkit side i am getting array count as zero.I do not know where i went wrong?any help will be appreciated.thanks in advance
I would guess that you have some trouble in your iOS app function. I think your reply closure is most likely not being called. I would try to simplify your logic first to make sure the reply is actually coming back correctly. Then you can work on passing the data correctly. I would simply the logic to the following first:
catecontroller.swift
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
WKInterfaceController.openParentApplication(["dummy": "dictionary"]) { reply, error in
println("Reply: \(reply)")
println("Error: \(error)")
}
}
AppDelegate.swift
func application(
application: UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!)
{
// Worry about this logic later...
reply(["test": "value"])
}
Once you have this simplified version passing the data correctly with no errors, then you can add the data passing logic.