Problem retrieving data from array iphone? - iphone

I declared a nsmutablearray and assign some object but when retrieving object it gives memory location.Here is the code.
NSMutableArray *arrCondiNum = [[NSMutableArray alloc] init];
[arrCondiNum addObject:#"2"];
[arrCondiNum addObject:#"4"];
[arrCondiNum addObject:#"6"];
[arrCondiNum addObject:#"8"];
for(int i = 0;i<[arrCondiNum count];i++){
NSLog(#"Array number %d",[arrCondiNum objectAtIndex:i]);
}
Output it gives
Array number 20820
Array number 20836
Array number 20852
Array number 20868

You add string in your array, then to display it, you have to use %#:
NSLog(#"Array number %#",[arrCondiNum objectAtIndex:i]);
In your code (%d), you display the address of the object.
%# will display the description of the ObjC object (return of -descriptionWithLocale: or -description)

Note that in case you want an array of numbers, use NSNumber instead:
[array addObject:[NSNumber numberWithInt:1]];
You still have to use the %# format specifier though (as NSNumber is a class-type) or retrieve the integer value using -intValue.

Try [[arrCondiNum objectAtIndex:i] floatValue] instead. NSArrays are keeping objects (pointers to objects to be more precise), not values. So the array will return the string object you created earlier. By sending this object the "floatValue" message, it will return its content represented as float value. You could use intValue as well to receive an integer.

just write like this..
NSLog(#"Array number %#",[arrCondiNum objectAtIndex:i]);
u have to print some text..so

Related

Storing a double array as NSNumber

I have a variable double * data = malloc(sizeof(double)) in objectiveC;
I am using this variable as an double array like data[] to store some data. Now I want to add this data variable (which is an double* array) as an object NSNumber in iOS. Any idea how I can turn it into iOS object likeNSNumber`?
You can use NSData to wrap an arbitrary byte buffer into an Objective-C object.
Use dataWithBytes:length: to create an NSData object from your double array, and bytes: or getBytes:length: to retrieve the data bytes back from the NSData object.
You cannot turn an array of primitives into one NSNumber. This does not make any sense.
You can, however, turn an array of doubles into an array of NSNumbers. Iterate through your double* array and add each number to an NSMutableArray as NSNumber using its class method numberWithDouble:.
Based on Mundi's answer, try this:
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < lengthOfDoublearray; i++) { // as premitive DataType array needs predefined length
[array addObject:[NSNumber numberWithDouble:data[i]]];
}
Here data is array of double (that you used).

Convert NSMutableArray into integer

i am working on xml parsing and getting three item numbers from the server 1
2 3, i have taken the three numbers into nsmutable array and assign them in delegate value mutable array, now i want to pass one number at a time into another function to get response from the server, so someone please let me know that how to convert NSMutable array value into integer while parsing into another function.
do like this
for(int i=0;i<[yourArrayFromXmlParsing count];i++)
{
int a=[[yourArrayFromXmlParsing objectAtIndexPath:i] intValue];
[obj function:a];
}
If u have added each number into the array u can access them by using the index of the array.
[[myArray objectAtIndex:i] intValue];
Assuming inside your array, you have NSNumber objects:
for (NSNumber *aNumber in yourArrayFromXmlParsing) {
resultFromServer = [self fooMethod:[aNumber intValue]];
}

Convert NSString to Integer in objective - c

I have an array that stores each digit of a phone number as a string. I then pass the phone number digit string as an argument for objectAtIndex: method of NSArray like this: [myArray objectAtIndex: [myString intValue]]; The compiler says I need to cast the String but I am already doing that. What is wrong?
Update:
Here's my actual line of code:
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[charHolder objectAtIndex:[[phoneNumDigits objectAtIndex:i]intValue]]];
This is where the error is, phoneNumDigits is an array of each digit of the phone number, charHolder is the array holding the array of letters associated with each digit.
Your question is a little confusing to me. Here is how I understand what you want to do:
You have an NSArray named phoneNumDigits. This array contains a few NSString objects. Each string is something like #"1" or #"4" and represents a single digit of a phone number.
Now you want to convert each of those digit strings to int or NSInteger and want to store these integers in another array.
If I understood you correctly, here is my answer:
You cannot exactly do what you want, because you can't put a simple data type like an int or a float into an NSArray.
That's why there is the wrapper class NSNumber. You can package a simple int in an NSNumber and store this NSNumber in an NSArray.
So to get your string digits from the phoneNumDigits into the tmp array you could use this code:
for (NSString *digitAsString in phoneNumDigits)
{
NSNumber *digitAsNumber = [NSNumber numberWithInt:[digitAsString intValue]];
[tmp addObject:digitAsNumber];
}
To get the ints out of the tmp-NSArray you would use
int digit = [[tmp objectAtIndex:idx] intValue];
I hope this helps, but I'm not sure I understand what you want to do here. I could be completely missing the point. Maybe you could share some more code.
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[charHolder objectAtIndex:[[phoneNumDigits objectAtIndex:i]intValue]]];
objectAtIndex returns a generic object (id). It has no idea that the object in the array is a string as it could be anything. So you need to cast it. Or to increase readabilty, create variables for them. Eg:
int phoneNumberDigit = [phoneNumDigits objectAtIndex:i];
NSArray *chars = [charHolder objectAtIndex:phoneNumberDigit];
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:chars];

Restrict Duplicate entry in NSArray

I have an array, which contains some duplicate entries.
Firstly, is there any way to restrict duplicate entries when data getting inserted?
Secondly, if an array already having duplicate values than in some other way, we can retrieve only unique values from that array, I heard about NSSet about this, but I have no idea how to use it.
Don't use an NSSet.
You can only insert elements upon creation and cannot change the elements contained after you have created it.
If you want to add and remove objects on the fly, you can use an NSMutableSet.
Here is a demo of how to use it both NSSet and NSMutableSet, then converting the NSSet back to an NSArray (incase you want to do that):
- (void) NSMutableSetPrintTest
{
NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
NSLog(#"Adding 5 objects (3 are duplicates) to NSMutableSet");
NSString *firstString = #"Hello World";
[mutableSet addObject:firstString];
[mutableSet addObject:#"Hello World"];
[mutableSet addObject:#"Goodbye World"];
[mutableSet addObject:#"Goodbye World"];
[mutableSet addObject:#"Goodbye World"];
NSLog(#"NSMutableSet now contains %d objects:", [mutableSet count]);
int j = 0;
for (NSString *string in mutableSet) {
NSLog(#"%d: %# <%p>", j, string, string);
j++;
}
NSLog(#"Now, if we are done adding and removing things (and only want to check what is in the Set) we should convert to an NSSet for increased performance.");
NSSet *immutableSet = [NSSet setWithSet:mutableSet];
NSLog(#"NSSet now contains %d objects:", [immutableSet count]);
int i = 0;
for (NSString *string in immutableSet) {
NSLog(#"%d: %# <%p>", i, string, string);
i++;
}
[mutableSet release]; mutableSet = nil;
NSLog(#"Now, if we are done with the sets, we can convert them back to an NSArray:");
NSArray *array = [immutableSet allObjects];
NSLog(#"NSArray contains %d objects", [array count]);
int k = 0;
for (NSString *string in array) {
NSLog(#"%d: %# <%p>", k, string, string);
k++;
}
}
NSMutableSet is probably the most logical thing to use.
However, be warned that a set does not maintain order of its elements (since a set, by definition, is unordered).
If that's a problem for you, then you have a couple of options:
duplicate set functionality with an NSMutableArray by invoking containsObject: before every call to addObject: (doable, but potentially slow, since arrays have O(n) search time)
use another object.
If you go with the second option, I would recommend taking a look at the excellent CHDataStructures framework, which has a subclass of NSMutableSet called CHOrderedSet, which is a set that maintains insertion order. (And since it's a subclass, it has the exact same API as an NSMutableSet)
If you've heard about NSSet, did you read the documentation? The API is similar to NSArray and very straightforward. Just like NSArray vs. NSMutableArray, you would use NSMutableSet if you need on the fly membership tests.

How can I access the int values of an object in an NSMutableArray?

I try to access some values in a NSMutableArray I created, but I only get some numbers (address?) if I try to access them.
I was able to initialize an array and can add and change objects with
[myNSMutableArray addObject:[NSNumber numberWithInt:10]]
and
[myNSMutableArray replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:47]
I also can print the value at index [0] with
NSLog(#"%#", [myNSMutableArray objectAtIndex:0]);
and I get 47 as expected.
But how can I access the integer value of the object in the array so I can save it tomyIntValue?
int myIntValue = [[myNSMutableArray objectAtIndex:0] intValue];
Just have a look at the NSNumber class reference