Appending Strings using NSStrings

Discussion in 'iOS Programming' started by rbeatajane, Jun 13, 2011.

  1. rbeatajane macrumors newbie

    Joined:
    Mar 11, 2011
    #1
    Hi,


    Code:
    char string1[512] = "Hello";
    	NSString* string = @"World!";
    	NSString* string2 = (NSString*)string1;
    	string2 = [string stringByAppendingString:string2];
    	NSLog(@"string:%@",string2);
    I have to append the strings. One is of c-type and the other is of NSObject type.
    How to append these two strings.I tried to append two NSStrings and I got the output.
    I'm getting an EXC_BAD_ACCESS when tried to execute the above code.
    How to make the char as an NSObject type.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    NSStrings are complex objects. They are not arrays of char. You absolutely completely and utterly cannot cast char* to NSString. If you look at the NSString documentation you will see numerous methods for working with CStrings.
     
  3. rbeatajane thread starter macrumors newbie

    Joined:
    Mar 11, 2011
    #3
    Yes

    NSString* string2 = [NSString stringWithUTF8String:string1]; solved my problem
     
  4. rbeatajane thread starter macrumors newbie

    Joined:
    Mar 11, 2011
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Did you look in the documentation? There is a whole group of methods titled "Getting C Strings". In addition the NSString documentation links to the String Programming Guide which I would expect you to have read. It has a section "Creating and Converting String Objects" which demonstrates answers to every question in this thread.

    The ability to use the documentation is a core programming skill that I would suggest you need to develop. Simply posting questions on forums before you have read the documentation is very lazy.
     
  6. rbeatajane thread starter macrumors newbie

    Joined:
    Mar 11, 2011

Share This Page