Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
I got an issue here. If I don't format it with the percent thingy I get the correct data. So I know the algorithm is correct. If I use the number formatter, I get bizarre data. For instance, one set of data should return 6% and I got 600%. I thought it was a digit padding issue, but I did other datasets that should had returned 1% and I got 750% instead.

Here is my code:

Code:
// ... snip

double Level = level( Points );
    double Progress = progress( Points);
   [lblLevel setDoubleValue:(Level)];
   [pbLevel setDoubleValue:(Progress)];
  
   NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
   [formatter setNumberStyle: NSNumberFormatterPercentStyle];
   // Any other format settings you want
   NSString* formattedNumber = [formatter stringFromNumber: [NSNumber numberWithDouble: Progress]];
   [lblPercent setStringValue:(formattedNumber)];

// ... snip

I am used to coding in C#/C++ so I am used to the old hungarian/camel text style of variable names. I was told by others I don't follow the Cocoa paradigm.

So since it works w/o using the number formatter, as if I just used a standard setIntVal on the label, it works fine. I just wished I can concatenate the double/int with char *! I am obviously new to this so I am eager to learn. Thank you for any help!
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
I have never used NSNumberFormatter before but I would have written something like below. But NSNumberFormatter might be a better way to go.

Code:
double Level = 23.45;
NSString *string = [NSString stringWithFormat:@"%.2f%@",Level, @"%"];
[lblPercent setStringValue:string];
//Result 23.45%

That is just off the top of my head if I needed to add a % sign in.

Just my 2 cents.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
You shouldn't capitalize the first letter of the name of anything other than a class name.

Also, you seem to have a large amount of completely unnecessarily ()'s.

Anyways, I think it might be helpful for you to know that in C, a percent symbol in a string should be escaped like this: "%%"

Edit: I didn't read your post very well, clearly. Sorry, maybe someone else will be more helpful.
 

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
I have never used NSNumberFormatter before but I would have written something like below. But NSNumberFormatter might be a better way to go.

Code:
double Level = 23.45;
NSString *string = [NSString stringWithFormat:@"%.2f%@",Level, @"%"];
[lblPercent setStringValue:string];
//Result 23.45%

That is just off the top of my head if I needed to add a % sign in.

Just my 2 cents.


This actually worked for what I needed, so thank you for that!

You shouldn't capitalize the first letter of the name of anything other than a class name.

Also, you seem to have a large amount of completely unnecessarily ()'s.

Anyways, I think it might be helpful for you to know that in C, a percent symbol in a string should be escaped like this: "%%"

Edit: I didn't read your post very well, clearly. Sorry, maybe someone else will be more helpful.

The capital letters were variables since I accidentally used the same names for functions. The parentheses are functions since a variable is getting populated from a function, hence the parentheses.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
The parentheses are functions since a variable is getting populated from a function, hence the parentheses.

These parentheses are all superfluous:
Code:
   [lblLevel setDoubleValue:(Level)];
   [pbLevel setDoubleValue:(Progress)];

...
   [lblPercent setStringValue:(formattedNumber)];


Regarding NSNumberFormatterPercentStyle, see Data Formatting Guide : Number Formatters, heading "Percentages".
 

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
Thanks!

Thank you for the link. Does Apple have a full PDF of everything rather than just chapters/units? I am learning MacOS development first before I move onto iOS. I guess though there are much differences like how I used NSTextField for my outlets I think for iOS they use UIText or something? Where can I find documentation on the differences between the two environments?

Thanks :)

:apple:
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
Thank you for the link. Does Apple have a full PDF of everything rather than just chapters/units? I am learning MacOS development first before I move onto iOS. I guess though there are much differences like how I used NSTextField for my outlets I think for iOS they use UIText or something? Where can I find documentation on the differences between the two environments?

Thanks :)

:apple:

The difference is OS X uses Cocoa and App Kit while iOS uses Cocoa Touch and UIKit. Different names for libraries that have basically the same capabilities (although I'm finding WebView seems to be worlds more capable than UIWebView... That's an exception to a general rule.)

If you're not yet familiar with C, you should definitely learn it first. If you're already familiar with C, I would suggest learning iOS first rather than OS X, especially if that's your end goal, because iOS has a lot more beginner friendly materials available and because the frameworks strike me as less redundant (and so less confusing to learn.)
 

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
Yes my end goal is iOS. I thought MacOS would be a better starting point but I'm beginning to learn it should had been the other way around.

My background is C++ so I have it's philosophy on objects and whatnot. I never picked up on the tokens that scanf/printf uses whenever I tried looking at pure C.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
Yes my end goal is iOS. I thought MacOS would be a better starting point but I'm beginning to learn it should had been the other way around.

My background is C++ so I have it's philosophy on objects and whatnot. I never picked up on the tokens that scanf/printf uses whenever I tried looking at pure C.

Learning pure C probably isn't necessary, then, if you already know C++.

Duncan C's post over at iPhone Dev SDK might be helpful for you:
http://iphonedevsdk.com/forum/iphone-sdk-development/100555-transition-from-c-to-objective-c.html
 

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
Thank you for that! I really appreciate it. Any books you can recommend for MacOS and/or iOS?
 

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
Do you mind if I ask you a second question?
I have a second set of NSTextFields. I have a submit/reset button on the form as well. However, I like the buttons to be disabled if the textfields are empty, as if they fill it in, it enables, if they delete it, it redisables.

I am so used to Visual Studio in C# where I just add events and be done. So I am rather confused with the Xcode paradigm of doing things.

Anyway, this is what I did....
I made the outlets for the textfield, I made an action for editChange

and it seems not to be working. I already made the two fields disabled in the Interface Maker.

But it seems not to work. Any suggestions is also greatly appreciated!

Thanks so much!

Code:
- (IBAction)editingChanged
{
   NSUInteger lenUser = [[txtUser stringValue] length];
   NSUInteger lenPass = [[txtPass stringValue] length];
  //[btnReset setState:NSOnState];
   
   
   [lblDebug setIntegerValue:lenUser]; // get the value for debugging
   
   
   if ( lenUser > 0 && lenPass > 0 )
   {
      [btnLogin setEnabled:YES];
      [btnReset setEnabled:YES];
   }
   else
   {
      [btnLogin setEnabled:NO];
      [btnReset setEnabled:NO];
   }
}
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
I believe Cocoa Bindings would make short (an codeless) work of that task, although I don't have much experience with it... I think it'd entail binding each text field's value to the Burton's enabled field (more enabled fields will be created for each one you use) and then I think there will be a toggle somewhere to pick whether they're joined together with logical ANDs or logical ORs.

I'm giving a deliberately crappy answer that I didn't research particularly well. Google some of the keywords from my post, if you don't understand what I'm saying.
 

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
No worries, the code I typed in was from Googled code, but it was more of UI stuff for iOS and I converted it to NS so as for my code I am not sure where it is failing I guess it isn't entering the outlet/action. I think I have the outlet going both directions, perhaps that isn't allowed? These drawings is still very awkward for me, since I am used to Windows style of development from work and hate to say it, but adding a method from the method explorer and adding the code to the function is a bit easier. And again, i am also more fluent in C++ than C, but the Objective-C where it uses Smalltalk style messaging will take some getting used to. Thanks though.
 

jweinraub

macrumors 6502
Original poster
Jun 26, 2007
371
219
Sol III
Okay, not sure what I did here, but I had so many open tabs but I got it to work.

Code:
- (void)controlTextDidChange:(NSNotification *)notification
{
  // NSLog(@"textDidChange");
   NSUInteger lenUser = [[txtUser stringValue] length];
   NSUInteger lenPass = [[txtPass stringValue] length];
   
   if ( lenUser > 0 && lenPass > 0 )
   {
      [btnLogin setEnabled:YES];
      [btnReset setEnabled:YES];
   }
   else
   {
      [btnLogin setEnabled:NO];
      [btnReset setEnabled:NO];
   }

}

I put the little line thingy to the AppDelegate rather than appcontroller.
Declared the function in the .h file obviously.
And it actually worked when I compiled it.
What I don't get is, the reset button programmatically deletes the contents of the field, but I would think the field, no the value should be zero, by nature, should then automatically lock up the cells, but it didn't because I never typed it (the backspace I assume) because if I do delete it manually it does lock up again. So, naturally I put the button code to lock up in the reset area.

I think my mini tutorial app is good enough, I can probably "graduate" and convert this beast into an iOS app since that was my original goal.

Thanks for all your help, it definitely got me where I needed to be so I definitely learned something!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.