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;
}
Related
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.
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.
I'm trying to include the elements of an array in the NSString that's returned by the -description method in my class. No clue how to do this in Objective-C...in Java there's string concatenation or StringBuilder, what's the equivalent in Obj-C?
TIA..
Just use NSArray's componentsJoinedByString: method with whatever you want between them as the argument.
NSString *elementsSquishedTogether = [myArray componentsJoinedByString:#""];
NSString *connectedByACommaAndSpace = [myArray componentsJoinedByString:#", "];
If you have a C array, you can turn it into an NSArray with NSArray *converted = [NSArray arrayWithObjects:yourCArray count:yourArrayCount].
The title of your thread talks about C arrays, so here's a modification of jsumners' answer that will deal wiith C arrays.
myArray is assumed to be an ivar declared thusly:
int* myArray;
storage for myArray is assumed to be malloc'd at some point and the size of it is in an ivar declared:
int myArraySize;
The code for description goes something like
- (NSString *)description
{
NSMutableString *returnString = [[[NSMutableString alloc] init] autorelease];
for (int i = 0 ; i < myArraySize ; i++)
{
if (i > 0)
{
[returnString appendString: #", "];
}
[returnString appendFormat: #"%d", myArray[i]];
}
return [NSString stringWithFormat: #"[%#]", returnString];
}
There are variations. The above version formats the string with bracket delimiters and commas between elements. Also, it returns an NSString instead of an NSMutableString which is not a big deal, but I feel that if you say you are returning an immutable object, you probably should.
The following could should "build" a string representation of your array. Notice that it is using the -description method of the objects in the array. If you want something different you will have to make the necessary change.
- (NSString *)description: (id) myArr {
NSMutableString *returnString = [[[NSMutableString alloc] init] autorelease];
for (int i = 0, j = [myaArr count]; i < j; i++) {
[returnString appendString: [[myArr objectAtIndex: i] description]];
}
return [NSString stringWithString: returnString];
}
Edit:
As JeremyP said, I answered this using Objective-C arrays. I guess I just forgot the question when I started writing my code. I'm going to leave my answer as an alternative way to do it, though. I've also fixed the return string type from a mutable string to an immutable string (as it should be).
I have a dictionary object that I am pulling data out of. The field is supposed to be a string field but sometime all that it contains is a number. I get the info using:
NSString *post = [[temp objectAtIndex:i] valueForKey:#"POSTDESCRIPTION"];
So it is going into a string object. However, when I try to assign that to a cell's text via:
cell.textLabel.text = post;
I get a the following error:
'NSInvalidArgumentException', reason: '*** -[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x4106a80'
2009-10-20 13:33:46.563
I have tried casting it with the following ways to no avail:
NSString *post = [[[temp objectAtIndex:i] valueForKey:#"POSTDESCRIPTION"] stringValue];
NSString *post = (NSString *)[[temp objectAtIndex:i] valueForKey:#"POSTDESCRIPTION"];
cell.textLabel.text = [post stringValue];
cell.textLabel.text = (NSSting *)post;
What am I doing wrong?
Your dictionary doesn't contain an NSString. If you'd like the string representation of the object, you could call the object's description selector, e.g.:
NSString *post = [[[temp objectAtIndex:i] valueForKey:#"POSTDESCRIPTION"] description];
terry, jason and the other answers & comments are correct. what you're attempting would be like trying to cast an apple into an orange.
conveniently, NSNumber does have a stringValue method. so try this:
NSString *post = [[[temp objectAtIndex:i] valueForKey:#"POSTDESCRIPTION"] stringValue];
only do this if you know for sure it'll always be an NSNumber.
otherwise, you can try the rather hacky and inelligant:
NSString *post = [NSString stringWithFormat:#"%#",[[[temp objectAtIndex:i] valueForKey:#"POSTDESCRIPTION"] description];
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!