I have this error:
[__NSArrayI length]: unrecognized selector sent to instance 0x8d06040
When I try and set a NSArray to the cell using the code:
cell.textLabel.text=[message objectAtIndex:indexPath.row];
Here is my code that starts as a NSDictionary:
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:LoginResponse options:0 error:&error];
NSLog(#"responseDataAfterNSDICT: %#", jsonDict);
//------------------------------------------
keys = [jsonDict allKeys];
values = [jsonDict objectsForKeys:keys notFoundMarker:[NSNull null]];
message = [values valueForKey:#"fromuser"];
NSLog(#"%#", message);
This is what my log is for the NSLog(#"%#", message);
2014-01-31 12:52:39.899 ArialCraft[16694:70b] (
(
),
(
KyleUnrau
),
(
FixableMass09
)
)
It works with another NSArray, what am I doing wrong?
I think you need one more subscript:
cell.textLabel.text = [[message objectAtIndex:indexPath.row] firstObject];
cell.textLabel.text is looking for an NSString object, not an NSArray object.
Related
I have an NSArray that looks like this
NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(#"main: %#", [dataDict valueForKey:#"main"]);
main: [{"city": "dallas", "description": "this is my test description"}]
And I am trying to turn it into a NSDictionary so I get get element like [mainDict objectForKey#"city"]
So I tried this
NSLog(#"%#", [[mainArray objectAtIndex:0] objectForKey:#"city"]);
Thinking it would get the first element in mainArray and then the object that I want but I keep getting this error
[__NSCFString objectAtIndex:]: unrecognized selector sent to instance
How can I make the NSAarry into a NSDictionary?
Thanks
Create a for loop for list when you get response in [{},{}] format that means its a list of elements . Or you can ask your API developer to correct the error .
Any ways simple solution here is :
for (NSDictionary *d in [dataDict valueForKey:#"main"]){
NSlog (#"%#",d);
NSlog (#"%#",[d objectForKey :#"city"]);
}
Your error is saying mainarray is not a NSArray, it is NSString. So please check like this
NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(#"main: %#", [dataDict valueForKey:#"main"]);
if ([[dataDict valueForKey:#"main"] isKindOfClass:[NSArray class]]) {
NSArray *mainArray = [dataDict valueForKey:#"main"];
NSLog(#"City == %#",[[mainArray objectAtIndex:0] objectForKey:#"city"]);
}else{
NSLog(#"mainarray is kind of -> %#", NSClassFromString([dataDict valueForKey:#"main"]));
}
NSArray *mainArray = #[#{#"city":#"dallas", #"description":#"this is my test description"}];
NSLog(#"%#", [[mainArray objectAtIndex:0] objectForKey:#"city"]);
no problem ....
I am using for in loop to fetch data from plist.For in loop is correct as it is printing value. But,suddenly, it is showing following exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x97a14b0'.
Code:
NSString *sss=[[NSBundle mainBundle]pathForResource:#"s" ofType:#"plist"];
NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:sss];
for(NSArray *arr in [dic allKeys])
{
NSLog(#"%#",arr); // Ii is printing value
NSLog(#"%#",arr[0]); // It is showing exceptions
}
Plist:
There is some problem with NSLog(#"%#",arr[0]);. I want to print values of first index of arrays a and b.
here [dic allKeys] will be keys in dic that are NSString like "a", "b" etc.
so for your plist the code should be,
NSString *sss=[[NSBundle mainBundle]pathForResource:#"s" ofType:#"plist"];
NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:sss];
for(NSString *key in [dic allKeys])
{
NSLog(#"%#",key);
NSArray *value = [dic objectForKey:key];
NSLog(#"%#",value[0]);
}
Im getting a JSON as below from a web service
data = {
following = 1;
};
message = "You are now following";
status = 1;
and I am trying to loop it using the following code (in order to get the value of the "following" key)
for(NSDictionary *item in datarecieved){
int placeName = [item valueForKey:#"following"];
NSLog(#"FOLLOWING VALUE %i", placeName);
}
But I am getting an exception - "uncaught exception of type NSException"
You have to do like this
for(NSDictionary *item in datarecieved){
if([item class] == [NSDictionary class])
{
int placeName = [item valueForKey:#"following"];
NSLog(#"FOLLOWING VALUE %i", placeName);
}
}
because there are two other key in response and they dosen't contain following key
Try it
int placeName = [datarecieved[#"data"][#"following"] intValue];
Once you deserialize the JSON it will be NSDictionary, so you don't need to use a loop (unless you need to loop through all keys)
In your case if you just want value for following key, you can do the following
NSNumber *following = [datarecieved valueForKeyPath:#"data.following"];
NSLog(#"FOLLOWING VALUE %#", following);
First of all, you need valid JSON. Look into the OP comments, there is a good address for understanding JSON.
Then you first must parse the JSON data. Assuming datareceived is of class NSData:
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:datareceived options:nil error:&error];
if(error) {
NSLog(#"parsing error: %#", error);
}
else {
for(NSString *key in [dict allKeys]) {
NSLog(#"%# = %#", key, [dict objectForKey:key]);
}
}
The exception you are doubtless getting is unrecognized selector sent to instance 0x12345678 and is because item does not support that selector.
You need to check that item is actually an NSDictionary before calling valueForKey: (you should be using objectForKey: anyway):
for(NSDictionary *item in datarecieved){
if ([item isKindOfClass:[NSDictionary class]]) {
int placeName = [item objectForKey:#"following"];
NSLog(#"FOLLOWING VALUE %i", placeName);
}
}
(you probably want to break at some point once you've found what you're looking for as well).
I have a problem with my FCR. I want to show my datas on Labels but FCR doesnt allow me.There is my codes..
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"faturaAdi == 'ADSL' "];
I have fetched only 1 data which fatura name is ADSL.. and I want to write this faturaAdi on my label but i couldn't do that.
NSArray *array = [[NSArray alloc]init];
array = self.fetchedResultsController.fetchedObjects;
_lblFaturaAdi.text = [self.fetchedResultsController.fetchedObjects valueForKey:#"faturaAdi"];
NSLog(#"%#",[array valueForKey:#"faturaAdi"]);
NSLog shows Takip[967:60b] (
ADSL
)
When i put the
_lblFaturaAdi.text = [self.fetchedResultsController.fetchedObjects valueForKey:#"faturaAdi"];
app crash with this error ;
-[__NSArrayI length]: unrecognized selector sent to instance 0xa18c740
how can i solve this ?
Your code should change to:
NSArray *array = self.fetchedResultsController.fetchedObjects;
if (array.count > 0) {
self.lblFaturaAdi.text = [[array objectAtIndex:0] valueForKey:#"faturaAdi"];
NSLog(#"%#", _lblFaturaAdi.text);
} else {
NSLog(#"no results found");
}
Because you don't need to initialise an array before replacing it and you need to index into the array before you can extract the value.
When I try to print array of json values in log, I get addresses instead of values. Here's how I coded.
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:jsonArray.count];
NSMutableArray *anotherTempArray = [NSMutableArray arrayWithCapacity:jsonArray.count];
NSDictionary *dict;
for(dict in jsonArray)
{
NSString *projectName = dict[#"Name"];
NSString *urlText = dict[#"Url"];
NSLog(#"Url text in array = %#", urlText);
NSString *attch = dict[#"attachmentes"];
NSLog(#"Attached url in array = %#", attch);
NSString *projID = dict[#"ProjectID"];
NSLog(#"Project ID in array = %#", projID);
SaveAttachment *saveAt = [[SaveAttachment alloc] initWithName:projectName withList:#"View" withAttachment:#"View"];
[tempArray addObject:saveAt];
SaveProjectId *saveProj = [[SaveProjectId alloc] initWithProjectId:projID];
saveProj.projectId = projID;
[anotherTempArray addObject:saveProj];
}
array = tempArray;
[self.tableViewProject reloadData];
NSLog(#"Array of project IDs === %#", anotherTempArray); //Get values (array of project ids here.
}
Replace
SaveProjectId *saveProj = [[SaveProjectId alloc] initWithProjectId:projID];
saveProj.projectId = projID;
[anotherTempArray addObject:saveProj];
with
[anotherTempArray addObject:projID];
This is because your anotherTempArray contains objects of SaveProjectId ie, everytime in for loop you are adding saveProj object not projID. Thats why your array showing SaveProjectId objects.
If you want to directly save them, then use the below modification
[anotherTempArray addObject:projID];
or you can use like(this is i would prefer)
NSLog(#"First project ID === %#", [anotherTempArray objectAtindex:0] projectId]);
You are storing SaveProjectId objects in the array, therefore when you print the content you see the address of those objects.
your "anotherTemoArray" is having objects of SaveProbectId so you have to pass object at index to SaveProjectId and then you can see the array information
When calling NSLog(#"Array of project IDs === %#", anotherTempArray); the -(NSString*)description method on each of the objects inside 'anotherTempArray' is being called.
In your case that means -(NSString*)description is being called on SaveProjectId objects. Override it to print out what you want... e.g.
-(NSString*)description {
return [NSString stringWithFormat:#"SaveProjectId: %#",self.projectId];
}