nsuserdefaults

Best way to pass variables from one Swift class to another?

你。 提交于 2019-12-08 15:29:12
问题 Suppose I define variable in one class, ViewController , and want to access that variable from another class, say BLEHandler . How do I go about doing that? I've had trouble with NSUserDefaults , so a method besides that would be greatly appreciated. 回答1: There are may ways you can pass value between viewControllers For example PrepareForSegue NSNotificationCenter Delegate Singleton Different way is used in different case. If you use prepareFoSegue - is used when you want to pass when perform

iphone dev - NSUserDefaults check for boolean existence

久未见 提交于 2019-12-08 15:22:53
问题 I've just added a settings bundle to my app and am having trouble reading the bool settings. I know that upon launch of the app, the settings are not read unless the user actually enters them - and that's what I am trying to capture. However, my code is simply capturing if the answer is NO OR they havent been set. I need to find out if they've been set, THEN set answers! setting code: BOOL playSound; BOOL playVibrate; //test for some defaults NSUserDefaults *prefs = [NSUserDefaults

Accessing NSUserDefaults Often

喜夏-厌秋 提交于 2019-12-08 15:21:24
问题 During a logic process in my app, I need to access the user preferences frequently, and a bunch of times 10~15 to determine what needs to be processed and how. May this question is not about performance, but about doing it correctly. Currently I'm doing a [[NSUserDefaults standardUserDefaults] valueForKey:...] each time I need to request a value. Is this correct? I think that "saving" the user defaults as an ivar could reduce extra work, but then I wonder if this won't have sync problems,

NSUserDefaults, NSCoder, Custom Class - iPhone app question

☆樱花仙子☆ 提交于 2019-12-08 14:44:10
问题 I'm having an error and I guess I'm doing something wrong in the following process. Firstly, I have a class Contacts: @interface Contact : NSObject<NSCoding> { @private ABRecordRef ref; NSString *first; NSString *last; bool selected; NSString *phoneNumber; } And in Contact's implementation, I have: - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:first forKey:@"First"]; [encoder encodeObject:last forKey:@"Last"]; [encoder encodeObject:[NSNumber numberWithInteger:

How can I save an array to UserDefaults, and then retrieve that array and print [duplicate]

☆樱花仙子☆ 提交于 2019-12-08 14:13:59
问题 This question already has answers here : Best Practice using an Array of Strings with NSUserDefault (2 answers) Closed 2 years ago . Here is my code. It seems an error. Can anyone please help. class PopUpVC: UIViewController { var myArray1 = ["1","2","3"] var myArray2 = [String]() UserDefaults.standard.set(myArray1, forKey: "array") override func viewDidLoad() { super.viewDidLoad() self.myArray2 = (UserDefaults.standard.string(forKey: "array") as? [String])! print(self.myArray) } 回答1: class

saving custom object which contains another custom object into NSUserDefaults

有些话、适合烂在心里 提交于 2019-12-08 12:46:27
问题 I understand how to store a custom object in NSUser Defaults but when i tried to implement saving an object with an object of different type inside it (it is called composition if i'm not mistaken) following the same steps for inner object as i did for the first one i got runtime error. Could you please minutely describe steps that i have to undertake in order to save and retrieve everything correctly 回答1: All your objects should implement NSCoding protocol. NSCoding works recursively for

How to retrieve string from UserDefaults in Swift?

橙三吉。 提交于 2019-12-08 12:07:09
问题 I have a textField for the user to input their name. @IBAction func nameTextField(sender: AnyObject) { let defaults = NSUserDefaults.standardUserDefaults() defaults.setObject("\(nameTextField)", forKey: "userNameKey") } Then I recall the inputted name in ViewDidLoad with: NSUserDefaults.standardUserDefaults().stringForKey("userNameKey") nameLabel.text = "userNameKey" What am I doing wrong? Result is simply "userNameKey" every time. I'm new to this, thanks! 回答1: You just have to assign the

Questions about NSUserDefaults

此生再无相见时 提交于 2019-12-08 10:42:33
问题 I have a couple of questions about NSUserDefaults in Mac OS X: When does the NSUserDefaults use the dictionary provided by registerDefaults? Only the first time the application is opened or every time the application is opened? Where is the information from NSUserDefaults stored? How can I reset NSUSerDefaults? Thanks! 回答1: Only the first time. But you can force an application to reuse the defaults with the terminal. ~/Library/Preferences/YourIdentifier.plist (e.g. com.apple.finder.plist)

NSUserDefaults “Pause” / Crash on iOS8

一个人想着一个人 提交于 2019-12-08 08:48:59
问题 Ok, so I launched an app last week before iOS8 came out. Everything was working fine in iOS7 and below. But now since people have updated to iOS8 my app is pausing/crashing for no reason. I came to terms that it is when I set an NSMutableArray to the NSUserDefaults, it pauses. Please note, the NSMutableArray is an array of NSStrings. BUT (this is weird) if I breakpoint skip through the code it works and I get no pause. Here is the function that it is blowing up... -(void)UpdateMyAgenda:

How can I save a second row of UIPickerView to NSUserDefaults?

送分小仙女□ 提交于 2019-12-08 06:45:54
问题 I have pickerView with two rows and I need to save position of both to NSUserDefaults. I have successful saved first row with this code: - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSInteger selectedRow = [thePickerView selectedRowInComponent:0]; [[NSUserDefaults standardUserDefaults] setInteger:selectedRow forKey:@"picker"]; } And I don't get how to add a second one. With this code I bring back the position: -(void