NSnumber in to NSstring - nsstring

Stock *stockObject;
stockObject = [[Stock alloc]initWithIssuingCompany:addCompany stockName:#"one" stockPrice:[NSNumber numberWithFloat:150.3]];
[availableStocks addObject:stockObject];
float myfloat = [s.stockPrice floatValue];
NSLog(#"singleton#%f", myfloat);
Try this code but it will crush on:
float myfloat = [s.stockPrice floatValue];
no error.
Can some one help me?

shouldn't it be [stockObject.stockPric floatValue] ?

Related

Addition between Label and Textfield

I am trying to add the value of my textfield plus the value of my label and place it in another label.
-(IBAction)finalTipTotalSelection:(id)sender
{
NSString *billFinalAmount = [billAmountTextField text];
float billFinalAmountFloat = [billFinalAmount floatValue];
NSString *tipTotalAmount = [resultLabel text];
float tipTotalAmountFloat = [tipTotalAmount floatValue];
float finalAmountShow = billFinalAmountFloat + tipTotalAmountFloat;
NSString *finalResult = [NSString stringWithFormat:#"$ %0.2f", finalAmountShow];
[finalTotalLabel setText:finalResult];
}
I am creating floatValues for the Strings and then in a float adding the other floatValues together, and finally displaying it in a label. The only issues is when I run this code the finalTotalLabel (shows the final amount) only shows the value of the billAmountTextField. So if the billAmountTextField = 187.82 and the tipTotalAmount was 10, the finalTotalLabel would show 187.82 (not 197.82). I cant seem to find my mistake. Thanks for your help!
It is caused by the tipTotalAmount having the $ character at the beginning of the string. The resulting float value for the tip amount will be 0.0 because:
floatValue... returns 0.0 if the receiver doesn’t begin with a valid
text representation of a floating-point number.
You can take a look at the class reference of NSString for the explanation of the return value of floatValue.
Try to filter out the non decimal and dot character first from the NSString instance before passing the message floatValue to it, for example:
NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString:#"."];
[_alnum formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
NSString *newTipTotalAmount = [[tipTotalAmount componentsSeparatedByCharactersInSet:
[_alnum invertedSet]]
componentsJoinedByString:#""];
float tipTotalAmountFloat = [newTipTotalAmount floatValue];
Here the newTipTotalAmount will be "39.19" compared to the tipTotalAmount which is "$ 39.19" and passing the floatValue message to newTipTotalAmount will give you the correct float value.

Declaring an NSDictionary with an NSString as its name

Ok, so I would like to create an NSDictionary, but the pointer should be followed by the value of an NSString that has been put in by the user. Is this possible? I imagine it would be something along the lines of this...
someNSString = _someTextField.text;
NSDictionary * {someNSString} = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:someUnimportantVariable], #"someUnimportantKey",...nil]
Thanks in advance, I realize that this is either completely not possible or there is a pretty simple solution. Either way, I'm sorry if I wasted your time.
you could try creating a "dictionary of dictionaries". something to the effect of:
someNSString = _someTextField.text;
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:someUnimportantVariable], #"someUnimportantKey",...nil] forKey:someNSString];
and if you wanted to get back that dictionary
NSDictionary someNSStringDictionary = [myDictionary objectForKey:someNSString];
let me know if it helps in any way.

OCMock is only effective once, strange, why? Or what's wrong on my side?

I want to do mock for TnSettings, yes, it works if code by the following method, the problem is that we need to do write mock code for each case, if we only mock once then execute more than one case, then the second will report exception. I use the latest OCMock V2.01.
My question is that why OCMock has such restriction? Or is it my fault not to use it correctly?
Any idea or discussion will be appreciated, thanks in advance.
- (void) testFormattedDistanceValueWithMeters {
mockSettings = [OCMockObject mockForClass:[TnSettings class]];
mockClientModel = [TnClientModel createMockClientModel];
[[[mockClientModel expect] andReturn:mockSettings] settings];
[[[mockSettings expect] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];
NSNumber *meters = [NSNumber numberWithDouble:0.9];
distance = [NSString formattedDistanceValueWithMeters:meters];
STAssertEqualObjects(distance, #"0.9", #"testformattedEndTimeForTimeInSeconds failed");
//------------- Another case -----------------
mockSettings = [OCMockObject mockForClass:[TnSettings class]];
mockClientModel = [TnClientModel createMockClientModel];
[[[mockClientModel expect] andReturn:mockSettings] settings];
[[[mockSettings expect] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];
meters = [NSNumber numberWithDouble:100.9];
distance = [NSString formattedDistanceValueWithMeters:meters];
STAssertEqualObjects(distance, #"101", #"testformattedEndTimeForTimeInSeconds failed");
}
Not sure I understand your question or your code fully. I suspect that you stumbled over the difference between expect and stub, though.
Is this what you had in mind?
- (void) testFormattedDistanceValueWithMeters {
mockSettings = [OCMockObject mockForClass:[TnSettings class]];
mockClientModel = [TnClientModel createMockClientModel];
[[[mockClientModel stub] andReturn:mockSettings] settings];
[[[mockSettings stub] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];
NSNumber *meters = [NSNumber numberWithDouble:0.9];
distance = [NSString formattedDistanceValueWithMeters:meters];
STAssertEqualObjects(distance, #"0.9", #"testformattedEndTimeForTimeInSeconds failed");
meters = [NSNumber numberWithDouble:100.9];
distance = [NSString formattedDistanceValueWithMeters:meters];
STAssertEqualObjects(distance, #"101", #"testformattedEndTimeForTimeInSeconds failed");
}

Error adding NSString variable into plist file

I've a problem I'm struggling with for a lot of time...please help.
I'm using the following line of code to add record to a plist file:
[data_appo setObject:#"string value" forKey:[NSString stringWithFormat:#"%i",(i-1)]];
and it works ok. Now,if I try to read the string from the file than adding it (I need to duplicate the value with a different key) as follows
NSString *string_appo=[data_appo objectForKey:[NSString stringWithFormat:#"%i",j]];
[data_appo setObject:string_appo forKey:[NSString stringWithFormat:#"%i",(i-1)]];
then I gest ERROR_EXEC and the app crashes...
Any idea???
Any help will be appreciated
Thanks in advance!
Have you saved the plist file after making the addition. I do this a lot and I do it like this
//find the plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Notes.plist"];
//set the value for the key
[notesData setValue:textView.text forKey:[NSString stringWithFormat:#"General notes %d",selectedIndex]];
//save the plist file
[notesData writeToFile: path atomically:YES];
//relese and reload it
[notesData release];
notesData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
I hope this helps!

NSTimeInterval from text input

I got a question for anyone with a spare moment and an answer...
I need to change an NSString value to input into the AVAudioPlayer start offset
eg. player.currentTime = 0; I want to change the '0' to a readable value or an NSString to a numeral value.....
NSString has a integerValue function. You can also create an NSString using it's format class function and pass it a format string like this: "%d" and then your player.currentTime.
int timeInput = [[textField text] integerValue];
label.text = [NSString stringWithFormat:#"%d", timeInput];

Resources