Swift 2 Migration Savecontext() in Appdelegate

Swift 2 migration saveContext() in appDelegate

The first of the two errors you provided is misleading, but the second is spot on. The problem is in !moc.save() which as of Swift 2, no longer returns Bool and is instead annotated throws. This means that you you have to try this method and catch any exceptions that it may emit, instead of just checking wether its return value is true or false.

To reflect this, a new project created in Xcode 7 using Core Data will produce the following boilerplate code which can replace the code you're using.

func saveContext () {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch {
// Replace this implementation 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.
let nserror = error as NSError
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
}
}

Swift 2.0 Migration errors

Cannot downcast from '[AnyObject]' to a more optional type '[NSManagedObject]'

In Swift 1.2, executeFetchRequest(:_) returned [AnyObject]?. In Swift 2, it returns [AnyObject] because the new try… syntax returns a non-optional.

(In the case that the method would return nil, the method would not return at all, and control would move to the catch block.)


'NSMutableDictionary' is not convertible to '[NSObject : AnyObject]'

This means you're trying to insert something into an NSMutableDictionary that can't be converted to an Objective-C object. In your case, I think it's because error is a struct conforming to ErrorType, rather than an NSError object. Try adding error1 instead.


Call can throw, but it is not marked with 'try' and the error is not handled.

save() might throw an error so it needs to be executed with try, instead of being evaluated as a bool. As Martin R. points out in the comments, the answer to this question provides a complete solution so I won't rehash it here.

Binary operator '&&' cannot be applied to two Bool operands

Here is some code that should do the trick. Remember to preceed throw statements with try and catch them.

func saveContext () {
if let moc = self.managedObjectContext {
if moc.hasChanges {
do {
try moc.save()
} catch {
NSLog("Unresolved error \(error)")
abort()
}
}
}
}

supportedInterfaceOrientationsForWindow in Swift 2.0

Try new syntax:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return [.Portrait, .PortraitUpsideDown]
}

How to create managedObjectContext using Swift 3 in Xcode 8?

In Swift3, you can access the managedObjectContext via the viewContext as

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

This option is available if Core data was enabled when creating the project. However, for existing project that you want to include core data, go through the normal process of adding the core data and add the following code which will allow you to get the

lazy var persistentContainer: NSPersistentContainer = {

let container = NSPersistentContainer(name: "you_model_file_name")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {

fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

You will need to import the CoreData.

Note: For Swift3, the ManagedObject Subclass are generated automatically.
See more from WWDC 2016

SCNetworkReachabilityFlags bitwise AND comparison error with Xcode 7

As of Swift 2, you have to use .contains

private func isReachable(flags: SCNetworkReachabilityFlags) -> Bool {
return flags.contains(.Reachable)
}

Swift 2 language: AVAudio

I think you should use try and catch, because Swift 2.0 has an new error handling. AVAudioPlayer can trow errors and you should catch these errors.

var angryc:NSURL = NSBundle.mainBundle().URLForResource("MSoundEffect", withExtension: "mp3")!
do {
angry = try AVAudioPlayer(contentsOfURL: angryc, error: nil)
} catch _ {
fatalError ("Error loading \(angryc): \(error)")
}

another method

When you are sure that the AVAudioPlayer won't fail, you can also use try! If it fails, your app will crash!

Your code would look like this

var angryc:NSURL = NSBundle.mainBundle().URLForResource("MSoundEffect", withExtension: "mp3")!
try! angry = AVAudioPlayer(contentsOfURL: angryc, error: nil)

Source:
https://developer.apple.com/videos/wwdc/2015/?id=106 at 39:30

more info here about try catch

and here about AVAudioPlayer

Swift 2 - kCTForegroundColorAttributeName

Why not just use NSForegroundColorAttributeName?



Related Topics



Leave a reply



Submit