iPhone SDK: NSMutableArray unrecognized selector - iphone

I have an NSMutableArray which gets objects added to it from an NSDictionary.
These objects are values of either 1 or 0.
I am trying to loop through this NSMutableArray, to check each value. If it is 1, I set add a green tick image to an array, if it is zero, I add a red cross image to an array.
However I am getting the following error on checking the values of the NSMutableArray:
[__NSArrayI isEqualToString:]: unrecognized selector sent to instance
Here is my code:
facilitiesAvailable = [NSMutableArray new];
[facilitiesAvailable addObject:[[InfoDictionary valueForKey:#"wc-avail"]copy]];
[facilitiesAvailable addObject:[[InfoDictionary valueForKey:#"wifi_avail"]copy]];
tixArray = [NSMutableArray new];
for(NSString *avail in facilitiesAvailable) {
if([avail isEqualToString:#"1"]) {
[tixArray addObject:[UIImage imageNamed:#"greenTick.png"]];
} else {
[tixArray addObject:[UIImage imageNamed:#"redCross.png"]];
}
}
What am I doing wrong?

The message:
[__NSArrayI isEqualToString:]: unrecognized selector sent to instance
means that avail is an NSArray object and not an NSString as you expected, hence the error. You should check how you populate your InfoDictionary dictionary, since it comes from there.

Just out of curiosity, what happens if you replace your code with this:
for(int i = 0; i < [facilitiesAvailable count]; i++) {
NSString * avail = (NSString *)[facilitiesAvailable objectAtIndex:i];
if([avail isEqualToString:#"1"]) {
[tixArray addObject:[UIImage imageNamed:#"greenTick.png"]];
} else {
[tixArray addObject:[UIImage imageNamed:#"redCross.png"]];
}
}
This also ASSUMES that the objects being added to facilitiesAvailable are based on the objects in your InfoDictionary truly being NSString objects. This CASTS them to NSString objects, but you will get odd behavior if they arent truly that from the InfoDictionary.

Related

NSMutablearray define init issue

i have the following line of code
NSMutableArray *marray = [[NSArray arrayWithObjects: #"4", #"1", #"9", nil]mutableCopy];
and i want to replace it with the following line
NSMutableArray *marray = [[NSMutableArray alloc]initWithArray:garr];
where garr is global array from global method
the problem is that the code works fine when calling the first line but when using the second one the code crash , appreciate ur help and ideas thanks , iknow that the first one is NSArray but the garr variable source is NSMutable array
here is the code for garr
garr = [[NSMutableArray alloc]init];
for (int x = 0; x < 10; x++) {
[garr addObject:[NSNumber numberWithInt: arc4random()%200]];
here is the error msg
console error:2012-09-02 14:46:42.976 sort_alg[1561:207] -[NSCFNumber UTF8String]: unrecognized selector sent to instance 0x4b1a170 2012-09-02 14:46:42.978 sort_alg[1561:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber UTF8String]: unrecognized selector sent to instance 0x4b1a170' * Call stack at first throw: –
this is the code that generates the end value
NSString *element;
NSEnumerator *iterator = [marray objectEnumerator];
while ((element = [iterator nextObject]) != nil)
printf("%s ", [element UTF8String]);
printf("\n");
[marray release]; // array needs to be released!
[pool release];
thanks
Problem lies in printf("%s ", [element UTF8String]);.
NSNumber has no UTF8String method, only a stringValue. You can't printf it either, but you can NSLog("%#", [element stringValue]), or NSLog("%d", [element intValue]) if you know it's an int.

application crashes when appendString for NSMutableString is used

I'm new to objective-c and I decided to start by working through Stanford's CS193s 2010F Session's lectures/assignments.
I was working on the second assignment, and I was stuck when I had to return a NSString that combines(concatenates) every strings inside NSMutableArray. (The MutableArray consists only NSString at its each indices)
My approach was to use a for loop to pass through the MutableArray's indicies(in the code below, the MutableArray is 'anExpression' with type 'id'). I declared a NSMutableString and added NSString at each indices of 'anExpression' array. Here is the code:
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++){
[result appendString:[anExpression objectAtIndex:i]];
}
return result;
}
However, at
[result appendString:[anExpression objectAtIndex:i]];
xcode crashes with following error statements:
2012-07-17 01:44:51.014 Calculator[9470:f803] -[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0
2012-07-17 01:44:51.015 Calculator[9470:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0'
*** First throw call stack:
(0x13ca022 0x155bcd6 0x13cbcbd 0x1330ed0 0x1330cb2 0x12d2d18 0x13460d7 0x1397a8d 0x3a50 0x27ed 0x13cbe99 0x1714e 0x170e6 0xbdade 0xbdfa7 0xbd266 0x3c3c0 0x3c5e6 0x22dc4 0x16634 0x12b4ef5 0x139e195 0x1302ff2 0x13018da 0x1300d84 0x1300c9b 0x12b37d8 0x12b388a 0x14626 0x1ed2 0x1e45 0x1)
terminate called throwing an exception
I looked through apple's developer's document, saw 'NSString stringWithFormat:' method, and decided to use this method instead:
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++){
[result appendString:[NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]]];
}
return result;
}
and it works now.
now I'm confused why second code works but the first doesn't.
I thought appending string only fails(and crashes) when it's passed a nil...
Is there something I'm missing?
Thank you in advance :)
It looks like the contents of anExpression are instances of NSNumber instead of NSString. The error you are getting is a hint as to how appendString works; it's first step is obviously to ask the passed "string" how long it is (presumably so it can allocate enough memory). This is obviously not a method on NSNumber (hence the crash) and stringWithFormat is designed to do type-checking and be more flexible.
I suspect you could also use stringValue just as well:
[result appendString:[[anExpression objectAtIndex:i] stringValue]];
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++) {
[result appendString:[[anExpression objectAtIndex:i] stringValue]];
}
return result;
}
Converting it into string would help you!!
Use this function:
+ (NSString *)descriptionOfExpression:(id)anExpression
{
NSMutableString *result = [[NSMutableString alloc] init];
for (int i = 0;i<[anExpression count];i++)
{
NSString *str = [NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]];
if(str)
{
[result appendString:str];
}
//OR
//[result appendFormat:[NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]]];
}
return result;
}

EXC_BAD_ACCESS error in during fetching data from custome objects

I had stored custom objects data in Array. I am fetching data from Array of custom objects in a function. when I am calling function for first time it is working good but When I am calling it again and again I am getting EXC_BAD_ACCESS.
Here is function details.
-(void) facebookDisplayFunction:(int)atIndex {
FacebookWallData *wall = (FacebookWallData *)[facebook_wallDataArray objectAtIndex:atIndex];
NSString *friendID= wall.actor_id;
NSString *linkFetch= wall.permalink;
NSString* postID=wall.postId;
NSNumber *countNumber;
NSString *friendName=#"";
NSString* profileThumImage=#"";
for(int i=0; i< [facebook_LikesArray count];i++) {
FacebookLikes* countValues=[[FacebookLikes alloc]init];
countValues=[facebook_LikesArray objectAtIndex:i];
// NSLog(#" postId_wall %# LikePostId = %#",postID,countValues.PostID);
if([postID isEqualToString:countValues.PostID]) {
countNumber=countValues.Count;
if(countNumber>0)
friendID=countValues.Friends;
[countValues release];
break;
}
[countValues release];
}
for(int i=0;i< [facebook_FreindsArray count];i++) {
FacebookFreinds* friendsRecord=[[FacebookFreinds alloc]init];
friendsRecord=[facebook_FreindsArray objectAtIndex:i];
if([friendID isEqualToString:friendsRecord.UID]) {
friendName=friendsRecord.name;
profileThumImage=friendsRecord.pic_smal;
[friendsRecord release];
break;
}
[friendsRecord release];
}
// Adding values in table //
[imageData addObject:#"facebook.png"];
[tableList addObject:wall.messages];
[profileUserName addObject:friendName];
[linksOfFacebookData addObject:linkFetch];
[RetweetAndLikeData addObject:#"5"];
[favedProfileThumb addObject:profileThumImage];
[twitterPostID addObject:#""];
[eachPostUID addObject:friendID];
[wall release];
}
And here I am calling function.
[self facebookDisplayFunction:0];
[self facebookDisplayFunction:0]; // EXC_BAD_ACCESS error here.
Why are you allocating an object like this FacebookLikes* countValues=[[FacebookLikes alloc]init] and then assigning to this same variable the instance inside the array with this code countValues=[facebook_LikesArray objectAtIndex:i] and later on you release it with this [countValues release]? You don't know what you are doing.
Try changing this:
FacebookLikes* countValues=[[FacebookLikes alloc]init];
countValues=[facebook_LikesArray objectAtIndex:i];
to this
FacebookLikes* countValues = [facebook_LikesArray objectAtIndex:i];
and remove all occurrences of [countValues release]. Do the same for the friendsRecord in the second for-loop. Also, what is the [wall release]? Remove it!
You should not allocate any of these objects because you are actually obtaining them from that array, and not creating a new instance. That just creates a leak in your code. You should not release any of these objects because they are retained by the array, and it is responsible for releasing them whenever they are removed from the array or after the array is destroyed/deallocated. Please, rtfm
If you get the error on the line:
[self facebookDisplayFunction:0];
it seems to me that most likely the object pointed by self has been deallocated. So, the problem would not be in facebookDisplayFunction...
Could you review how you create the object pointed by self, or post the code if you need more help?

Weird NSDictionary crash

I have the following code:
NSDictionary *dict = [[NSDictionary alloc]
initWithObjectsAndKeys:myarray1, #"array1", myarray2, #"array2" nil];
NSArray *shorts =[[dict allKeys] sortedArrayUsingSelector:#selector(compare:)];
for (NSString *dir in shorts) {
NSArray *tempArr = [dict objectForKey:dir];
for (NSString *file in tempArr ) {
NSLog(#"%#", file);
}
}
Where myarray1 and myarray2 are NSArrays.
When I execute the code the application crashes with:
-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1d134
This is apparently the tempArr, which is not recognized as an NSArray. I know that [dicFiles objectForKey:dir] returns an id type object, but as a generic type, I cannot get what I'm doing wrong.
You haven't included the code that initializes myarray1 and myarray2, but apparently one or both of them are instances of NSString rather than NSArray. You can check that after retrieving one of the objects from the array as follows:
if (![tempArr isKindOfClass:[NSArray class]])
{
NSLog(#"Unable to process temp array because it's an instance of %#", [tempArr class]);
}
else
{
// for loop code goes here...
}

iPhone, show NSMutable Array containing NSStrings in UIPickerView

I have a method with the following code:
NSMutableArray *pickerArray = [[NSMutableArray alloc] init];
int i;
for(i = 1; i <= 7; i++) {
NSString *myString = [NSString stringWithFormat:#"%#", i];
[pickerArray addObject:myString];
}
for(i = 1; i <= 7; i++) {
NSString *fieldName = [[NSString alloc] initWithFormat:#"column%d", i];
[self setValue:pickerArray forKey:fieldName]; // setValue or initWithArray ???
[fieldName release];
[pickerArray release];
}
srandom(time(NULL));
When I build the application everything builds correctly but it crashes on start in the console i get the following error:
* -[NSCFString superview]: unrecognized selector sent to instance 0x380da90
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString superview]: unrecognized selector sent to instance 0x380da90'
If instead of using an array containing strings I use UIImageView containing UIImages then everything works correctly...
I only would like to populate my picker with an array of numbers from 1 to 50...
Any help would be really appreciated... this thing is driving me mad :)
I don't think you want [myString release]; in your first for loop as the string you create is auto-released (rule of thumb, anything that is created without alloc, init, or new is auto-released)
Problem solved.... was something to do with the number of element in the pickerview, nothing to do with the method itself! Thanks anyway!