NSString *list = #"Norman, Stanley, Fletcher";
NSMutableArray *a2 = [[NSMutableArray alloc] init];
//a2= [list componentsSeparatedByString:#", "];
[a2 addObject:list];
//NSArray *listItems = [list componentsSeparatedByString:#", "];
NSLog(#"array is %#",a2);
NSLog(#"array o %#",a2[0]);
how to get a2[0]=Norman , a2[1]=stanley???
Trying this
NSArray *listItems = [list componentsSeparatedByString:#", "];
[a2 addObject:list];
NSLog(#"array o %#", [a2 objectAtIndex:0]);
When you create a mutable array you have to add objects to the array. You can't set the mutable array equal to a normal array (its returned by componentsSeparatedByString:)
NSString *list = #"Norman, Stanley, Fletcher";
NSMutableArray *a2 = [[NSMutableArray alloc] init];
[a2 addObjectsFromArray:[list componentsSeparatedByString:#", "]];
NSLog(#"array is %#",a2);
NSLog(#"array o %#",[a2 objectAtIndex:0]);
Related
I have an array and I want to split that array into 3 parts or 3 arrays.
1st array contains -> AppName
2nd array contains -> Description
3rd array contains -> Icon
Here is the json array I want to split,
Deviceinfo = (
{
Appname = App;
Description = "This is test app";
Icon = "57.png";
}
);
}
Here is my code for this,
NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];
for (int i = 0; i < [json count]; i++) {
NSArray *tempArray = [[json 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: %#", json, firstArray, secondArray, thirdArray);
I observe a crash in the code at this line,
NSArray *tempArray = [[json objectAtIndex:i]componentsSeparatedByString:#""];
I don't understand what is going wrong here. Any pointers to solve this issue?
I think you can using below code i hope this help's you :-
NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e];
//here is first i load with Dicutionary bcz if into your Json you have may be multiple Dictuionary so you then you can load purticular dictionary as bellow line
EDIT
NSArray * responseArr = jsonArray[#"Deviceinfo"];
firstArray = [responseArr valueForKey:#"Appname"];
secondArray = [responseArr valueForKey:#"Description"];
thirdArray = [responseArr valueForKey:#"Icon"];
if you have multiple Deviceinfo dictionary in to your Json then you could use For loop
// NSArray * responseArr = jsonArray[#"Deviceinfo"];
// for (NSDictionary *dict in responseArr) {
// [firstArray addObject:[dict valueForKey:#"Appname"];
// [secondArray addObject:[dict valueForKey:#"Description"];
// [thirdArray addObject:[dict valueForKey:#"Icon"];
// }
NSMutableArray *firstArray = [[NSMutableArray alloc]init];
NSMutableArray *secondArray = [[NSMutableArray alloc]init];
NSMutableArray *thirdArray = [[NSMutableArray alloc]init];
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:&error];
NSArray * tempArray = jsonArray[#"Deviceinfo"];
for (NSDictionary *list in tempArray)
{
[firstArray addObject:[list objectForKey:#"Appname"];
[secondArray addObject:[list objectForKey:#"Description"];
[thirdArray addObject:[list objectForKey:#"Icon"];
}
Then try
NSLog(#"yourArray: %#\nfirst: %#\nsecond: %#\nthird: %#", tempArray, firstArray, secondArray, thirdArray);
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://url.to.your.json"]];
NSArray *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *appNameArray = [NSMutableArray array];
NSMutableArray *discriptionArray = [NSMutableArray array];
NSMutableArray *iconArray = [NSMutableArray array];
for(NSDictionary *dictionary in jsonObjects)
{
[appNameArray adddObject:[dictionary valueForKey:#"Appname"];
[appNameArray adddObject:[dictionary valueForKey:#"Description"];
[appNameArray adddObject:[dictionary valueForKey:#"Icon"];
}
I have a NSMutableArray of NSStrings where each element of array has the format equal to #"key is 1::value is 1". Now I want to store string part coming before "::" in an array1 and string part coming after "::" in an array2. How can I do that?
Here is the Code :
NSArray *temp = [YourString componentsSeparatedByString:#"::"];
NSString *str1 = [temp objectAtIndex:0];
NSString *str2 = [temp objectAtIndex:1];
But prior to accessing the Objects in the Array .. check for whether it contains the value.
Try this ::
NSString *s = #"key is 1::value is 1";
NSArray *a = [s componentsSeparatedByString:#"::"];
NSLog(#" -> %# --> %#", [a objectAtIndex:0], [a objectAtIndex:1]);
use this:
[array1 addObject:[[YourString componentsSeparatedByString:#"::"] objectAtIndex:0]];
[array2 addObject:[[YourString componentsSeparatedByString:#"::"] objectAtIndex:1]];
The key to splitting your strings is to use the componentsSeparatedByString: method on NSString to separate your string into an NSArray. Read the docs on how this method acts with blank strings etc, but it's what you'd use.
You said you have an array of strings, so the basic implementation would involve iterating over that array and adding each element to the two other arrays.
NSMutableArray *arrayOfStrings = [NSMutableArray array];
NSMutableArray *array1 = [NSMutableArray array];
NSMutableArray *array2 = [NSMutableArray array];
for (NSString *string in arrayOfStrings)
{
NSArray *components = [string componentsSeparatedByString:#"::"];
if ([components count] == 2)
{
NSString *obj1 = [components objectAtIndex:0];
NSString *obj2 = [components objectAtIndex:1];
[array1 addObject:obj1];
[array2 addObject:obj2];
}
}
I found the way. I will keep adding the beforeString and afterString in array1 and array 2 respectively while iterating the elements of original array
for(int i=0;i<self.originalArray.count;i++)
{
NSString *temp=[self.originalArray objectAtIndex:i];
NSRange r = [temp rangeOfString:#"::"];
NSString *beforeString = [temp substringToIndex:r.location];
NSString *afterString = [temp substringFromIndex:r.location+2];
[array1 addObject:beforeString];
[array2 addObject:afterString];
}
I want to replace an array object in NSMutableArray obect.
I am getting oldArray from _alertsArray and changing its value and want to replace oldArray with newArray . but there is no effect!!
NSArray *oldArray = (NSArray *)[_alertsArray objectAtIndex:[indexPath row]];
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:oldArray copyItems:YES];
[newArray replaceObjectAtIndex:3 withObject:#"YES"];
[_alertsArray replaceObjectAtIndex:[indexPath row] withObject:newArray];
NSMutableArray *newArray = [_alertsArray mutableCopy];
[newArray replaceObjectAtIndex:3 withObject:#"YES"];
_alertsArray = newArray;
JSON format code looks like this:
{
"rates": {
"amc": "201",
"hyd": "500.50",
"guj": "200.10",
"afgd": "400"
}
}
After parsing JSON values the above code array returns:
array = [values valueForKey:#"rates"];
which array return
{
amc = "201";
hyd = "500.50";
guj = "200.10";
afgd = "400";
...........etc
}
But i want to print in UITableView look amc:201
How can i do this?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
values = [responseString JSONValue];
array = [[NSMutableArray alloc] init];
NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
NSMutableArray *arrValues = [[NSMutableArray alloc] init];
array =[values valueForKey:#"rates"];
NSLog(#"array values:--> %#",array);
// NSLog(#"values:--> %#",values);
// NSLog(#"Particular values:--> %#",[[values valueForKey:#"rates"] valueForKey:#"AED"]);
tempDict1 = (NSMutableDictionary *)array;
NSArray *arr;// =[[NSArray alloc]init];
arr = [[tempDict1 valueForKey:#"rates"] componentsSeparatedByString:#";"];
NSLog(#"arr-->%#",arr);
NSString *subStar = #"=";
[arrTitle removeAllObjects];
[arrValues removeAllObjects];
for (int i=0; i<[arr count]-1; i++)
{
[arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
[arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
NSLog(#"arrTitle is:--> %#",arrTitle);
}
tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
array = [values valueForKey:#"rates"];
NSLog(#"tempDict--%#",[tempDict1 objectForKey:#"AED"]);
[array retain];
[tbl_withData reloadData];
}
try:
NSMutableArray * array2=[[NSMutableArray alloc]init];
[array addObject: [[array objectAtIndex:0] objectForKey:#"amc"]];
[array addObject: [[array objectAtIndex:0] objectForKey:#"hyd"]];
[array addObject: [[array objectAtIndex:0] objectForKey:#"guj"]];
[array addObject: [[array objectAtIndex:0] objectForKey:#"afgd"]];
now put array2 values in your table cell.
like:
cell.text=[array2 objectAtIndex:indexPath.row];
try this:-
NSDictionary *dict =[values valueForKey:#"rates"];
NSString *str=[dict valueForKey:#"amc"];
You need to do this:-
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
values = [responseString JSONValue];
NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
NSMutableArray *arrValues = [[NSMutableArray alloc] init];
NSDictionary *dict =[values valueForKey:#"rates"];
NSString *str=[dict valueForKey:#"amc"];
rest you can do according to your requirements.
Return type for [values valueForKey:#"rates"] is a dictionary not array of values.
NSDictionary *dict = [values valueForKey:#"rates"];
Now you can refer get values within dictionary using objectForKey
Or else if you want to store in array.
NSMutableArray *array = [NSMutableArray array];
for (NSString *key in [dict allKeys])
{
array = [dict objectForKey:key];
}
So finally array has all the values within dictionary.
// returns no of rows
-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section
{
return [array count];
}
// contents of row will be
-(UITableViewCell*) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
static NSString *CellIdentifier = #"test";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSString *value = [self. array objectAtIndex:indexPath.row];
cell.textLabel.text = value;
return cell;
}
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* users = [json objectForKey:#"Users"];
NSEnumerator* enumerator = [users objectEnumerator];
id element;
NSMutableArray *results;
Result *fetchedResults;
while(element = [enumerator nextObject]) {
// fetchedResults = [[Result alloc] init]; // i have tried commenting/uncommenting
fetchedResults.name = (NSString *)[[element objectForKey:#"User"] objectForKey:#"name"];
fetchedResults.email = (NSString *)[[element objectForKey:#"User"] objectForKey:#"name"];
NSLog(#"%#", fetchedResults.name);
[results addObject:fetchedResults];
NSLog(#"%#", (NSString *)[[element objectForKey:#"User"] objectForKey:#"name"]); // this returns valid dump
}
NSLog(#"%d", [results count]); // returns 0
I don't understand wht's wrong here. I have searched through numerous tutorials and resources don't seem to get what's wrong here.
EDIT:
NSLog(#"%#", fetchedResults.name); // dumps null
You forgot to allocate your results array NSMutableArray *results = [[NSMutableArray alloc] init] this should help.
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* users = [json objectForKey:#"Users"];
NSMutableArray *results = [[NSMutableArray alloc] init];
for (id object in users) {
Result *fetchedResults = [[Result alloc] init];
fetchedResults.name = (NSString *)[[element objectForKey:#"User"] objectForKey:#"name"];
fetchedResults.email = (NSString *)[[element objectForKey:#"User"] objectForKey:#"name"];
NSLog(#"%#", fetchedResults.name);
[results addObject:fetchedResults];
}
NSLog(#"%#", (NSString *)[[element objectForKey:#"User"] objectForKey:#"name"]);
}
NSLog(#"%d", [results count]); // returns 0