Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NSNumber numberWithBool in Swift

I submitted my Version 2 of my app for review and it got rejected due to a high backup to iCloud - wasn't aware of this as I only have to pics in my app but anyway. Now I try to convert that code

NSError *error = nil;
    NSURL *databaseUrl = [NSURL fileURLWithPath:databasePath];
    BOOL success = [databaseUrl setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
    if(!success){
    NSLog(@"Error excluding %@ from backup %@", [databaseUrl lastPathComponent], error);
    }

to swift and I couldn't get it work... This is what I have so far...

func excludeFromBackup() {
    var error:NSError?
    var fileToExclude = NSURL.fileURLWithPath("path")
    var success:Bool = fileToExclude?.setResourceValue(NSNumber.numberWithBool(true), forKey: NSURLIsExcludedFromBackupKey, error: &error)
    if success {
        println("worked")
    } else {
        println("didn't work")
    }
}

As you see, I fail with the numberWithBool Value.

Can anyone help me? Did anyone convert it before?

Thanks in advance...

like image 426
Armin Scheithauer Avatar asked Jan 09 '23 18:01

Armin Scheithauer


1 Answers

Some Swift types (Int, Bool, String, ...) are automatically bridged to the corresponding Objective-C type, so you can simply write:

let success = fileToExclude.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey, error: &error)

(More details in Working with Cocoa Data Types.)

like image 131
Martin R Avatar answered Jan 16 '23 00:01

Martin R