What to do about "objc_msgsend"?

Discussion in 'iOS Programming' started by wionjay, Oct 6, 2009.

  1. wionjay macrumors newbie

    Oct 6, 2009
    I am not understanding why I am getting this or what it means. I am fairly new to programming for iPhone and Mac OS.

    What does this mean?

    I have a NSString set but as NSString* myString[20]; I have assigned its value in one function and when I try to retrieve it again in another function I get this error, usually when I try to say NSString* myString2 = myString[10];.

    I know I am doing something wrong but am not sure of what, I've been actually trying to figure out this problem for a few hours and I keep searching the web and different forums to see who else has experienced this as I do find allot of people have experienced this I still don't understand why it is caused since most don't say why with anything near what I am getting wrong.

    I hope this is enough to give a reference so someone can at least tell me what retarded code I am doing as a programmer.

    Thank in advance!
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Are you retaining myString between method calls?
  3. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    You cannot use array subscript notation with NSStrings; they are not an array of characters like regular C strings. If you would explain what you are trying to do we can help you more.

    Clarification: If you're intent is to access the NSString like an array of characters. Using array subscripts are fine if you want an array of NSStrings (as demonstrated by dejo below)

    Are the arrays you're declaring globals, defined at the beginning of a function or malloc/calloc'd? If they are not globals or malloc'd, the moment you go out of scope the memory is deallocated and the value is lost. If you try to access the pointer outside of the valid scope you'll either crash (if you're lucky) or get garbage data.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Actually you can, as long as wionjay's intention is to create a C array of NSStrings, rather than trying to restrict the length of the NSString. But it's not normally done this way and using NSArray is probably preferred.

    I did the following quick test:
    NSString *myString[20];
    for (int i = 0; i < 20; i++) {
    	myString[i] = [NSString stringWithFormat:@"String %d", i];
    NSString *myString2 = myString[10];
    NSLog(@"%@", myString2);
    ...and, lo and behold, the output was "String 10"
  5. whooleytoo macrumors 603


    Aug 2, 2002
    Cork, Ireland.
    It looks like you're trying to create a fixed-length string for later use.

    If so, it's done very differently on Cocoa.
    - You don't allocate strings using that notation
    - If you want the change the value, you need to use mutablestrings
    - You don't need to provide a size, the strings will grow as required.

    If you want to create a string, there are several options:

    // A simple, constant string
    NSString* constantString = @"This is a constant string";
    // Creating a string, then assigning it a value
    NSString* nameString;
    nameString = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    // Creating a mutablestring (a string whose value can be changed afterwards)
    // Possibly what you need?
    NSMutableString* editString;
    editString = [NSMutableString stringWithCapacity: 10];
    [editString setString: @"Yellow!"];
    [editString setString: @"No, red!"]; // See, you can change mutablestrings!
    [editString setString: @"No, yellow! yellow! yellow"]; // This works, the string will grow as required!
    The error you're getting is probably because you're trying to call a NSString method on myString, which isn't a proper NSString.
  6. wionjay thread starter macrumors newbie

    Oct 6, 2009
    Thank you all!

    I just wanted to say thanks, I now have a better idea of what's going on and it gave me a little more to research from.

Share This Page