Expected Declaration Error using Swift
The problem is that you need to put your code inside a method. All you need is to move it to viewDidLoad() or any other method.
The problem is that you need to put your code inside a method. All you need is to move it to viewDidLoad() or any other method.
One way of doing it might be to archive it (like with NSColor, though I haven’t tested this): NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color]; [[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@”myColor”]; And to get it back: NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@”myColor”]; UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
NSString *valueToSave = @”someValue”; [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@”preferenceName”]; [[NSUserDefaults standardUserDefaults] synchronize]; to get it back later NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@”preferenceName”];
ref: NSUserdefault objectTypes Swift 3 and above Store UserDefaults.standard.set(true, forKey: “Key”) //Bool UserDefaults.standard.set(1, forKey: “Key”) //Integer UserDefaults.standard.set(“TEST”, forKey: “Key”) //setObject Retrieve UserDefaults.standard.bool(forKey: “Key”) UserDefaults.standard.integer(forKey: “Key”) UserDefaults.standard.string(forKey: “Key”) Remove UserDefaults.standard.removeObject(forKey: “Key”) Remove all Keys if let appDomain = Bundle.main.bundleIdentifier { UserDefaults.standard.removePersistentDomain(forName: appDomain) } Swift 2 and below Store NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: “yourkey”) NSUserDefaults.standardUserDefaults().synchronize() Retrieve var returnValue: [NSString]? … Read more
In Swift 4 or higher, Use Codable. In your case, use following code. class Blog: Codable { var blogName: String? } Now create its object. For example: var blog = Blog() blog.blogName = “My Blog” Now encode it like this: if let encoded = try? JSONEncoder().encode(blog) { UserDefaults.standard.set(encoded, forKey: “blog”) } and decode it like … Read more
The code you posted tries to save an array of custom objects to NSUserDefaults. You can’t do that. Implementing the NSCoding methods doesn’t help. You can only store things like NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate in NSUserDefaults. You need to convert the object to NSData (like you have in some of the code) … Read more
You should always use Keychain to store usernames and passwords, and since it’s stored securely and only accessible to your app, there is no need to delete it when app quits (if that was your concern). Apple provides sample code that stores, reads and deletes keychain items and here is how to use the keychain … Read more
On your Player class, implement the following two methods (substituting calls to encodeObject with something relevant to your own object): – (void)encodeWithCoder:(NSCoder *)encoder { //Encode properties, other class variables, etc [encoder encodeObject:self.question forKey:@”question”]; [encoder encodeObject:self.categoryName forKey:@”category”]; [encoder encodeObject:self.subCategoryName forKey:@”subcategory”]; } – (id)initWithCoder:(NSCoder *)decoder { if((self = [super init])) { //decode properties, other class vars self.question … Read more
You can set an array to userDefaults, the implementation is very simple. – (void)viewDidLoad { [super viewDidLoad]; [self addTextToUserDefaults:@”hello”]; [self addTextToUserDefaults:@”how are you?”]; [self addTextToUserDefaults:@”hi”]; for (NSString *text in [self textsInUserDefaults]) { NSLog(@”%@”, text); } } – (void)addTextToUserDefaults:(NSString *)aText { NSMutableArray *texts = [[[NSUserDefaults standardUserDefaults] objectForKey:@”textArray”] mutableCopy]; if (!texts) { texts = [NSMutableArray new]; } … Read more
Let’s first get in one page. you have viewController and you are presenting another viewController from code? and you want to pass data of first viewController to second one? In your case for data passing there is no need of “user defaults” and “CoreData”. Since you are not using segues, You can simply do it … Read more