nsuserdefaults

What is the use of -[NSUserDefaults registerDefaults:]?

此生再无相见时 提交于 2019-12-17 04:28:01
问题 What is the difference between: [[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithObjectAndKey:anObject, @"something"]]; And this: [[NSUserDefaults standardUserDefaults] setObject:anObject forKey:@"something"]; 回答1: The difference is that the first code-snippet you register defaults that will be used when the user has not made any changes to the "property". So if you want to provide let's say a "property" with the key name 'Welcome message', you could

iOS 四种保存数据的方式!

柔情痞子 提交于 2019-12-15 19:09:18
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题。将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好。下面介绍一下数据保存的方式: 1.NSKeyedArchiver:采用归档的形式来保存数据,该数据对象需要遵守NSCoding协议,并且该对象对应的类必须提供encodeWithCoder:和initWithCoder:方法。前一个方法告诉系统怎么对对象进行编码,而后一个方法则是告诉系统怎么对对象进行解码。例如对Possession对象归档保存。 定义 Possession: @interface Possession:NSObject<NSCoding>{//遵守NSCoding协议 NSString *name;//待归档类型 } @implementation Possession -(void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:name forKey:@"name"]; } -(void)initWithCoder:(NSCoder *)aDecoder{ name=[[aDeCoder decodeObjectforKey:@"name"] retain]; } 归档操作:

Saving an array of NSURL to NSUserDefaults

女生的网名这么多〃 提交于 2019-12-14 03:57:21
问题 I have an array of NSURL items like this: var array = [NSURL]() I'm trying to save this to NSUserDefaults: let defaults = NSUserDefaults(suiteName: "group.a.project") defaults?.setObject(array, forKey: "NSURLarray") Will result in it crashing as NSURL can't be stored in NSUserDefaults. How can I save this array of NSURL's to NSUserDefaults, so that I can then access it and use the NSURL's in the array as required (saving an array of strings and then trying to use them as NSURL's will result

NSUserDefault Update Formula

末鹿安然 提交于 2019-12-13 23:08:21
问题 Have a question on how can I update my formula by NSUserDefault. So I have two text field which needs to keep my Formula uptodate. So the user types in there number (value) then that numbers needs to go to my formula but the formula only showing me the distance value :). 回答1: I tried your code with some changes. Here are my .h file and .m files. Try this. Now also I didn't understand what your trying to find out, but this code gives me the values not a nan . While you writing code, don't

Checkbox in UITableView not working when row is deleted

♀尐吖头ヾ 提交于 2019-12-13 22:16:24
问题 I have implemented a checkbox in UITableView controller, it works perfectly but when the checkbox are selected and the rows in the tableview are deleted and the when I add a new row to the tableview it gets selected. Here is a link to my project: https://files.fm/f/tepkz2du I think it has something to do with when a row is delete, the index path is messed up. UPDATED CODE: cellForRowAt: if let btnChk3 = cell?.contentView.viewWithTag(100) as? UIButton { btnChk3.addTarget(self, action:

How do I convert a UserDefault value from AnyObject? to an Int so I can save it

烈酒焚心 提交于 2019-12-13 19:55:18
问题 I'm trying to learn how to work with UserDefaults and can't figure out how to store one of the values I need to store. I have this enum: enum CalculationFormula: Int { case Epley case Brzychi case Lander case Lombardi case MayhewEtAl case OConnerEtAl } and this class with a property called 'selectedFormula' that I want to be able to store as a NSUserDefaults value: class CalculatorBrain: NSObject { var weightLifted: Double var repetitions: Double var oneRepMax: Double? var selectedFormula =

iOS Terminating app due to uncaught exception 'NSUnknownKeyException'

隐身守侯 提交于 2019-12-13 13:59:59
问题 I am pretty new to objective c and iOS programming, and I have this pretty strange error. The app in question initializes a NSMutableArray with a preset set of values of a custom type I made using NSObject. Which is manipulated by the app. If new values are added during app run time, they are saved using NSUserDefaults, and are brought up from NSUserDefaults along with the default values on next app open. This is the error I get: Terminating app due to uncaught exception

Settings bundle values returning nil

允我心安 提交于 2019-12-13 12:02:09
问题 I added a Settings bundle to my app and in Xcode it appears in the root of my project tree view. The Root.plist file looks like this: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>StringsTable</key> <string>Root</string> <key>PreferenceSpecifiers</key> <array> <dict> <key>Type</key> <string>PSGroupSpecifier</string> <key>Title</key> <string>Service</string> </dict>

What am I doing wrong with this NSMutableArray of structs in NSUserDefaults? “attempt to insert non-property list object”

心已入冬 提交于 2019-12-13 11:02:22
问题 I'm quite simply trying to save an NSMutableArray / NSArray of struct s in NSUserDefaults : typedef struct { int key; } provision; provision p1; p1.key = 4; NSValue *p1Value = [NSValue value:&p1 withObjCType:@encode(provision)]; provision p2; p2.key = 5; NSValue *p2Value = [NSValue value:&p2 withObjCType:@encode(provision)]; NSMutableArray *provisions = [[NSMutableArray alloc] init]; [provisions addObject:p1Value]; [provisions addObject:p2Value]; [[NSUserDefaults standardUserDefaults]

Xcode: Check if UIButton is hidden, using NSUserDefaults

a 夏天 提交于 2019-12-13 09:36:04
问题 How would I allow the app to save & load the current state of a button (i.e. if it is hidden, how would I save this, so that next time it will be hidden)? Also, I have it currently set so that when a button is pressed, all the other buttons become un-hidden. How can I set it so that certain ones don't become un-hidden if they have been set un-hidden in NSUserDefaults if you see what I mean? Thanks 回答1: You can check with Apple's documentation on NSUserDefaults to get any additional