How can I read Float from Core Data / iPhone? - iphone

I have Float values stored in Core-Data.
What is the code to use to read these values in an NSstring ?

Core-Data uses NSNumber objects to store the float value.
To get the 'raw' float value and put it into a string you would use something like this.
NSNumber *floatNumber = [managedObject valueForKey:#"myFloatValueKey"];
float myFloat = [floatNumber floatValue];
NSString *floatString = [NSString stringWithFormat:#"%f", myFloat];
Maybe a NSNumberFormatter would be useful.

Well, assuming that you mean your Core Data entity has an attribute of type float, you could simply access that field after you perform a fetch of that object.
[[self managedObjectContext] fetchObjectsForEntityName:#"EntityName" withPredicate:
#"(attribute LIKE[c] 'value') AND (attribute2 > %#)", someValue];
You could then put this in string format with this:
NSString* myNewString = [NSString stringWithFormat:#"%f", [[managedObject floatAttribute] floatVal]];

Related

Display NSNumber In Label

I am trying to display an NSNumber from an array with keys into an UILabel. Here is my current code: marblesNeeded.text = [[[records objectAtIndex:0] valueForKey: #"marblesneeded"] intValue];
I also get the error:
warning: Semantic Issue: Incompatible integer to pointer conversion assigning to 'NSString *' from 'int'
Thanks
You need to create an NSString to set the text of a UILabel.
marblesNeeded.text = [NSString stringWithFormat:#"%i",[[[records objectAtIndex:0] valueForKey: #"marblesneeded"] intValue]];
In the format %i denotes that you will be providing an integer value after the format.
Edit:
As some comments have noted NSNumber does have a stringValue, it does work but is not my personal preference because it gives you little control as to the format of the string. Consider this example.
NSNumber *number = [NSNumber numberWithFloat:3.25];
NSLog(#"%#",number.stringValue); // Will print 3.25
NSLog(#"%#",[NSString stringWithFormat:#"%i",number.intValue]); // Will print 3
Since the question envolved printing an intValue this more explicit format may be necessary.
marblesNeeded.text = [[[records objectAtIndex:0] valueForKey: #"marblesneeded"] stringValue];

Unable to convert string value into float

when i convert a string value into float it takes o.oooo. here is the code.
NSString *amt=txtTotalBill.text;
NSLog(#"%#",amt);
float amount = [amt floatValue]
NSLog(#"%f",amount);
NSString *insertData=[NSString stringWithFormat:#"insert into tbl_Bills(Amount,Note,Due_On) values ('%f')",amount];
[database executeQuery:insertData];
NSLog(#"inert query: %#",insertData);
NSLog(#"value inserted");
[table reloadData];
[database close];
everytime it takes amount as 0.0000.In this code amt value is correct but that string value doesnt convert into float.
just tried some of your codeblock and it works out fine for me. The error could be in the NSString itself. Perhaps its not passing in a totally numerical number. Try using a CGFloat as well, although this shouldn't change anything.
I assume you've alreayd ensured that no non-numeric characters can make their way into the NSString thats to be converted.
Can you let us know whats the output you got for the NSLog for the string?
Could you also try setting an output format for the float? e.g. '%3.3f' will display 3 numbers each before and after the decimal point.
Check txtTotalBill.text if the text is float number format.
If text is not float number format, [NSString floatValue] retuns 0.00000
Try this, i think this will work.
float amount = [txtTotalBill.text floatValue];
This works perfect
NSString *amt = txtTotalBill.text;
float amount;
amount = [amt floatValue];

nsmutablearray float object returns a zero

In the root model I have:
[self.rPrices replaceObjectAtIndex:0
withObject:[NSNumber numberWithFloat:(float)nR4]];
NSLog(#"%.2f", [self.rPrices objectAtIndex:0]);
where rPrices is NSMutableArray.
nR4 is not zero but the above NSLog(...); displays zero.
Thanks
Try this
NSLog(#"%.2f", [[self.rPrices objectAtIndex:0] floatValue]);
or alternativly just print the NSNumber as an object
NSLog(#"%#", [self.rPrices objectAtIndex:0]);
NSNumber is an object. So you can't print it using float format specifier "%f".
You can use "%#" or get the float value from it using -floatValue and print it using "%f".

How to create image and labels using location data stored in NSArray

i have to create an 2images and 3 labels by using code (cgrectmake)and i am having X location, y location, width and height all are stored in arrays(which i have retrieved from the web services)how can i create the image and labels can any one help me
You can join the elements of an array together with the NSString componentsJoinedByString class method:
NSString myString = [myNSArray componentsJoinedByString:#"x"];
where x is the characters you'd like to appear between each array element.
Edited to add
So in your newly-added code if these are the label values:
lbl = #"zero"
lbl1 = #"one"
lbl2 = #"two"
and you want to join them together with a space character then if you did this:
NSString *temp = [labelArray componentsJoinedByString:#" "];
NSLog(#"temp = %#", temp);
then this is what would be logged:
zero one two
Edited to further add
If you are instead trying to join the label values together to make xml elements then you might do something like this:
NSString *joinedElements = [labelArray componentsJoinedByString:#"</label><label>"];
NSString *temp = [NSString stringWithFormat:#"<label>%#</label>", joinedElements];
NSLog(#"temp = %#", temp);
then this is what would be logged:
<label>zero</label><label>one</label><label>two</label>
may be this is usefull to you.
NSString *str;
str = [arrayName objectAtIndex:i(Index NO)];
OK by this easily you can access object from the array. any type of object u can fetch this way only reception object type are change in left side.
Best of Luck.
Most objects have a -description method which returns a string representation of the object:
- (NSString *)description;
For example:
NSArray *array = [NSArray arrayWithObjects:#"The", #"quick", #"brown", #"fox", nil];
NSLog(#"%#", array); // prints the contents of the array out to the console.
NSString *arrayDescription = [array description]; // a string
It would help to know what you want to do with the string (how will you use the string). Also, what kind of objects do you have in the array?
In that case, Matthew's answer is one possibility. Another might be to use an NSMutableString and append the individual items, if you need control over how the string is created:
NSMutableString *string = [NSMutableString string];
if ([array count] >= 3) {
[string appendString:[array objectAtIndex:0]];
[string appendFormat:#"blah some filler text %#", [array objectAtIndex:1]];
[string appendString:[array objectAtIndex:2]];
}

Problem when converting NSString to NSNumber in iPhone

I am having problem when converting string (YaxisData) to NSNumber. I have to return a NSNumber for Core-plot to get the graph done but its not working. Here's the sample code
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num = [NSNumber numberWithDouble:[[YaxisData objectAtIndex:index] doubleValue]];
return num;
}
num returns junk data such as -1 or 993494949494 but when I log the double value of number, it prints the correct value. I am not able to return this double value as the function signature requires only the NSNumber to be returned.
NSLog(#"Number: %f", [num doubleValue]);
I am stuck here and would really appreciate any help in this regard. Thanks!
Would this give a better result somehow?
NSString *aString = [YaxisData objectAtIndex:index];
NSLog(#"%#", aString);
double value = [aString doubleValue];
NSLog(#"%f", value);
NSNumber *number = [NSNumber numberWithDouble:value];
NSLog(#"%#", number);
If not, could you show the results of the NSLog()'s?
I know that in practice it seems the same code, yet one might not be so sure that -objectAtIndex: always returns a string. It might be a localization issue as well (commas and dots for decimal separator might get mixed up, this would definitely mess up your results. In case of a localization issue, check the following link:
How to convert an NSString into an NSNumber