I am trying to write a method for a new class called “word”, which is a subclass of NSString. I want a method that will accept a NSString containing a single character and return the the place within the word of every instance of that string. I have this so far:
@implementation Word
-(NSMutableArray *)placeOfLetter: (NSString *)letterAsked;{
NSUInteger *len=(NSUInteger *)[self length];
int y=0;
char letter=(char)letterAsked;
for (NSUInteger *x=0; x<len; x++) {
if ([self characterAtIndex:*x]==letter){
[matchingLetters insertObject:x atIndex:y];
y++;
}
}
}
@end
however, xcode is telling me that I cannot put x as a the parameter for insertObject, because the “implicit conversion from NSUInteger to id is disallowed. How can I get around this?
The problem you’re encountering is stemming mostly from your treating
NSUIntegeras a pointer; you have some other casting problems as well. Try the following:letterAskedargument, then getting acharout of it, just get achar(orunichar) as your argument to begin with. You avoid theletter = (char)letterAskedconversion altogether.Don’t make
lena pointer. You may not need to declarelenat all. Consider writing yourforloop like:This also helps you in the
-characterAtIndex:call; you no longer need to dereferencexin order to get the character.Like Hot Licks said in the comments, use an NSNumber if you want a position inside an NSArray; you need numbers to be class instances to go in an NSArray instance. You can create an NSNumber out of
xlike this:Consider just using NSMutableArray’s
-addObject:method, rather than keeping track ofyand incrementing it each time. You’ll get the same result.