how to converts NSMutableArray values into NSString? [duplicate] - iphone

This question already has answers here:
How can I convert NSMutableArray into NSString?
(3 answers)
Closed 9 years ago.
hi all how to convert NSMutableArray values into the NSString
when i did as like
Webservices *details=[[Webservices alloc] init];
[details getMobileNumberDetails:phnotextfield.text];
NSLog(#"longitudes Arrays from %#",details.resultData);
"-91.57696007",
"10.343234",
"74.982343",
"76.464844",
"76.464844",
"2.256",
but when implemented like this
theCoordinate2.longitude = [[details.longarray objectAtIndex:0] floatValue];
NSLog(#"longitudes list from %#",theCoordinate2.longitude);
-91.57696007
but my intention is to assign all values to theCoordinate2.longitude so help me to store into string element in iphone.

You can use this code:
theCoordinate2.longitude = [[details.longarray objectAtIndex:0] stringByAppendingString:[details.longarray objectAtIndex:1];
//and so on for index 2,3,4 stringByAppendingString:[details.longarray objectAtIndex:2
NSLog(#"longitudes list from %f",theCoordinate2.longitude);

+1 to Wasif, but while he was composing his answer I was composing my own version of the code you could try:
NSMutableArray * longitudeArray = NULL;
NSString * detailsAsString = details.resultData;
if(detailsAsString)
{
NSArray * longitudeStringArray = [detailsAsString componentsSeparatedByString, #","];
if(longitudeStringArray && ([longitudeStringArray count] > 0))
{
longitudeArray = [[NSMutableArray alloc] initWithCapacity: [longitudeStringArray count]];
if(longitudeArray)
{
for(NSString * longitudeString in longitudeStringArray)
{
[longitudeArray addObject: [NSNumber numberWithFloat: [longitudeString floatValue]]];
}
}
} else {
NSLog( #"did not extract an array out of webservice data");
}
}

Related

Fetch NSarray from the NSDictionary of NSArray using objectForKey

I have a NSArray which contain n number of NSDictionary sampleArray =
(
{
0 = 0;
1 = 0;
},
{
0 = 86400;
1 = 2;
},
{
0 = 172800;
1 = 4;
},
{
0 = 259200;
1 = 5;
}
)
Now I need to fetch the NSArray for objectForKey 0 [sampleArray objectForKey:[NSNumber numberWithInt:0]], my result NSArray should be like
(0,86400,172800,259200) but I am unable to fetch the result and the app crashes.
Normally for NSDictionary, if key value is set using NSString valueForKey the above operation is performed successfully but if key value is set using an object like NSNumber objectForKey I am unable to perform the operation.
Please help me to get a solution, any suggestion would be appreciated!!
A very straight forward way if your keys are NSNumber objects:
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSDictionary *d in a) {
if (d[#0]) {
[result addObject:d[#0]];
}
}
THIS doesnt work - im sorry: I didnt see you didnt have Strings as keys + I didnt know KVC only works with strings
I leave it though
what you are looking for is
NSArray *zeros = [mainArray valueForKey:#"0"];
it gets "0" from each dict in array
What you have do is
NSMutableArray *arrResult = [[NSMutableArray alloc]init];
for(int i=0;i<[sampleArray count];i++)
{
NSString *strValue = [[sampleArray objectAtIndex:i] valueforkey:#"0"];
[ arrResult addobject:strValue];
}
let me know it is working or not!!!!
Happy coding!!!!!
here you can try NSArray *tempArray
for (NSDictionary *list in tempArray)
{
[firstArray addObject:[list objectForKey:#"0"];
[secondArray addObject:[list objectForKey:#"1"];
}

Tread 9: EXC_BAD_ACCESS [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I get this message "Tread 9: EXC_BAD_ACCESS (code=1, address=0x70000010) in this method (but this bug is being created only when in another thread file is being downloaded ):
- (NSMutableDictionary *) getDictionaryAllStatin:(sqlite3*)database
{
if (self._streetsArrey == nil || self._streetsArrey.count <= 0) {
[self getArrayAllStatin:database];
}
/*--------------------------------------------------------------*/
NSMutableDictionary *result1 = [[NSMutableDictionary alloc] init];
for (StreetData *street in _streetsArrey) {
NSString * name = [self deleteContractionWithText:street._name];
NSArray * arr = [name componentsSeparatedByString:#" "];
NSMutableArray *arrm = [[NSMutableArray alloc] initWithArray:arr];
arr = nil;
[arrm addObject:name];
for (NSString *txt in arrm) {
int lengthText = txt.length;
for (int i = 2 ; i <= lengthText; i++) {
NSString * key = [txt substringToIndex:i];
key = [key lowercaseString];
NSMutableDictionary *isSet = [result1 objectForKey:[NSNumber numberWithInt:[key hash]]];
if (isSet == nil) {
isSet = [[NSMutableDictionary alloc]init];
}
[isSet setObject:street forKey:[NSNumber numberWithInt:street._streetId]];
[result1 setObject:isSet forKey:[NSNumber numberWithInt:[key hash]]];
isSet = nil;
key = nil;
}
}
}
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
for (id key in result1) {
NSMutableDictionary *dictionary = [result1 objectForKey:key];
NSArray *arr = [dictionary allValues];
[result setObject:arr forKey:key];
arr = nil;
[dictionary removeAllObjects];
dictionary = nil;
}
[result1 removeAllObjects];
result1 = nil;
/*--------------------------------------------------------------*/
if (result.count > 0) {
_streetsDictionary = result;
result = nil;
return _streetsDictionary;
}else {
_streetsDictionary = nil;
return nil;
}
}
Why do I get this message?
How can I fix it?
The most likely cause for the crash is trying to access an object that has already been deallocated.
Since it seems that the failure arises on the line:
NSMutableDictionary *isSet = [result1 objectForKey:[NSNumber numberWithInt:[key hash]]];
I would suggest splitting the statement down to its component to try and track down which object could actually be the culprit:
NSInteger h = [key hash];
NSNumber n = [NSNumber numberWithInt:h];
...
but this bug is being created only when in another thread file is being downloaded
Also, check if the downloading code and the crashing code have anything in common. The former might be causing the deallocation of an object used in the second.
Hope it helps.

How to get last array values from NsmutableArray [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the last object of an NSArray
In my iPhone application, I want to retrieve the last array value from NSMutableArray:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:messages];
NSLog(#"array : %#", array);
In this "array". I having this below values.
hi, hello,
I want to take hello values only from the NSMutableArray.
How can I do this?
Try this
NSLog(#"array : %#",[array lastObject]);
It may help's you
you should follow indexpath at [array count] - 1 to obtain the last value.
For Ex,
NSString *str = [NSString stringWithFormat:"%#",[array objectAtIndex:[array count] - 1]];
NSLog("last object of an array = %#",str);
Enjoy Programming!
You can use:
NSString *str = (NSString *)[array lastObject];
[array lastObject] returns the last object of the array.
NSArray *arr=[NSArray arrayWithObjects:#"A",#"B",#"C", nil];
NSString *last=[arr lastObject];
NSLog(#"Last obj in string : %#",last);
Output :
Last obj in string : C

NSString comparison not working [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
IF clause is not validating NSString function return?
what I am trying to do is get items from an RSS feed and store them in arrays depending on their type. But the problem is when I compare the strings to separate the items nothing happens I have checked and the strings are exactly the same but it says they are not equal.
- (void)parserDidEndDocument:(NSXMLParser *)parser {
if (eventsList != nil) {
for (int i = 0; i<[eventsList count]; i++) {
NSMutableString* str = [[eventsList objectAtIndex:i] objectForKey:#"category"];
if ([str isEqualToString:#"normal"]) {
normalEvents = [[NSMutableArray alloc] init];
[normalEvents addObject:eventsList];
} else if ([str isEqualToString:#"sub"]) {
subEvents = [[NSMutableArray alloc] init];
[subEvents addObject:eventsList];
} else if ([str isEqualToString:#"main"]) {
NSLog(#"salve mundi");
mainEvents = [[NSMutableArray alloc] init];
[mainEvents addObject:eventsList];
} else {
NSLog(#"%#",str);
}
}
}
}
Using == you compare the objects' address. To compare the contents of two NSStrings you have to use the instance method [str isEqualToString:#"sub"] instead.
I would do some tests to ensure that the string is equal. For example, if it has whitespaces at the end or beginning, if it's uppercase, etc..
[[yourString lowercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
if([str isEqualToString:#"normal"])

how is the code dividing single array into multiple arrays? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How is the code for separation of single array into two arrays?
hi all i am getting below response into single array when i used the JSON parsing from my required URL but here i have to divide single array response into three array sets one set will have TB3257,TB3259,TB3261,TB3263,TB3257,TB3260 and second array set will have TB3258,TB3260,TB3262,TB3259,TB3258 and TB3261 and third array set will have TB3258. so how is the code for dividing single array into three array sets in iphone?
(
" TB3257 TB3258",
" TB3259 TB3260",
" TB3261 TB3262",
" TB3263 TB3259",
" TB3257 TB3258 TB3258",
" TB3260 TB3261"
)
I just tried the following code:
NSArray *yourArray = [NSArray arrayWithObjects:#"TB3257 TB3258", #"TB3259 TB3260", #"TB3261 TB3262", #"TB3263 TB3259", #"TB3257 TB3258 TB3258", #"TB3260 TB3261", nil];
NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];
for (int i = 0; i < [yourArray count]; i++) {
NSArray *tempArray = [[yourArray objectAtIndex:i]componentsSeparatedByString:#" "];
[firstArray addObject:[tempArray objectAtIndex:0]];
[secondArray addObject:[tempArray objectAtIndex:1]];
if ([tempArray count] == 3) {
[thirdArray addObject:[tempArray objectAtIndex:2]];
}
}
NSLog(#"yourArray: %#\nfirst: %#\nsecond: %#\nthird: %#", yourArray, firstArray, secondArray, thirdArray);
Output was:
yourArray: (
"TB3257 TB3258",
"TB3259 TB3260",
"TB3261 TB3262",
"TB3263 TB3259",
"TB3257 TB3258 TB3258",
"TB3260 TB3261"
)
first: (
TB3257,
TB3259,
TB3261,
TB3263,
TB3257,
TB3260
)
second: (
TB3258,
TB3260,
TB3262,
TB3259,
TB3258,
TB3261
)
third: (
TB3258
)
Hope it helps