Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 7    Views: 113

Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
07/18/15 02:58 PM (9 years ago)

NSUserDefaults question

Quick question of NSUserDefaults. I'm using Watchkit (but unrelated to the code) to grab the value in a label and show the same value in another view controller (the watch). The problem is, in the iphone app, everything shows perfectly, but in the second view controller, it does not. I'm using NSUserDefaults to save and load the values. All data in the second view shows as "0". I have a feeling it has to do with the %.0f variables, but all works fine in the app, just not when loading the other view. I've also tried %i, %d, %ld, %g, etc. Unfortunately, I haven't found a way to use NSLog with an app extension yet (like Watchkit). But I'm hoping the error will be obvious to some of you more advanced coders, since the code isn't very big at all. Here is the code I'm using: ================== .M file of my app ================== double step = [quantity doubleValueForUnit:[HKUnit countUnit]]; _lblSteps.text = [NSString stringWithFormat:@"%.0f",step]; NSLog(@"There are %.0f steps", step); [[[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.com.angryninjas.TripTips"] setInteger: step forKey:@"Steps"]; [[NSUserDefaults standardUserDefaults] synchronize]; ========================= .M file of watchkit extension view ========================= float count = [[NSUserDefaults standardUserDefaults] integerForKey:@"Steps"]; _lblSteps.text = [NSString stringWithFormat: @"%.0f Total Steps", count];
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
07/18/15 03:24 PM (9 years ago)
Ahhh.... got it sorted. Forgot the initWithSuiteName command when loading from the apple watch screen. With the Apple Watch (or any app extension), the App Group must be declared on both sides. ;) Out of curiosity, does anyone else have an apple watch, or messed with Watchkit yet? Just curious as to whom (if anyone) I could bounce some ideas off of or help out with the Apple Watch coding when needed.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
07/18/15 03:26 PM (9 years ago)
I'm not quite understanding some of the syntax... When I'm using 'stringwithFormat', it usually goes like this: [NSString stringWithFormat:@"my string and %@", anotherProperty]; So that the 'anotherProperty' Value is used by the '%@'. If I need to use the percent sign 'inside' the string, I duplicate it (%%). Cheers! -- Smug PS: Apple Watch? I still don't have an iPhone 5 yet. The wife gets one her birthday in a couple of months. But not me, lol!
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
07/18/15 03:30 PM (9 years ago)
Nope, not using the % inside the string (like %% does). The variable replaces the %.0f in each line. Luckily though, it didn't turn out to be a variable formatting issue, which I'm a bit weak on, just forgot to include the second App Group declaration.
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
07/18/15 03:32 PM (9 years ago)
The Watch is cool, but basically just a notification device that alerts you when somethings going on with your phone (email, text, call, etc). Yes, you can kind of reply to texts (via siri style voice to text), kind of answer a phone call (the speaker is crap... if you have any background noise, you won't be able to hear them, although the mic seems good because they can hear me just fine), but I use it mostly to read an incoming email or text without taking my phone out of my pocket. Also handy when driving and I don't always hear my phone. The thumping notification on the wrist is really good, you can actually feel it.
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
07/19/15 01:18 AM (9 years ago)
I've played with it a little bit in some tutorials.
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
07/19/15 09:25 AM (9 years ago)
Nice Nad. Do you have a watch? Did you do the tutorials in Objective C or Swift? Most examples I try to look up are in Swift, although I use objective C. I had few issues getting most data, but the heart rate really got me stumped. Couldn't find any code that worked for getting the beats per minute.
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
07/20/15 11:28 PM (9 years ago)
I did the tuts in swift. But I haven't touched swift since then. First I did the Ray Wenderlich videos then the bitfountain videos. Looking forward to watch os2. I don't have a device.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.