简体   繁体   中英

Using char in C, pointer issue, objective C

for (int i=0; i < strlen(chars1); i++) {
    //*buf=chars1[i];
    NSLog(@"Received %c\n", chars1[i]);
    ichar = [ NSString stringWithUTF8String:chars1[i] ];
    NSLog(@"Received ", ichar);
    counter=strlen(chars1)-i;
gnutls_record_send (session, [ichar UTF8String] , counter); 
}

I am trying to use this method to send information to my server but i keep getting an warning:

warning: passing argument 1 of 'stringWithUTF8String:' makes pointer from integer without a cast

the warning is for this line: ichar = [ NSString stringWithUTF8String:chars1[i] ];

is there anyway i can store chars1[i] into a temp char or string and the use it in my loop for sending?

UPDATED

I am just looking for a way to parse a NSString from starting character to end and then be able to use that without getting this warning:

makes pointer from integer without a cast

You can't parse UTF8 one character at a time (the correspondence is not one-to-one). The correct solution depends on the protocol you're parsing, which should specify how to determine when a string begins and ends. Follow the protocol to identify the completed string as a raw array of bytes and then parse it as UTF8 to covert it to a string.

You don't need to take individual element from char array and do all that. There is a class method in NSString,

+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
cString

    A C array of bytes. The array must end with a NULL character; intermediate NULL characters are not allowed.

//Use this 
    NSString* tempString = [NSString stringWithCString:chars1 encoding:NSUTF8StringEncoding];

The result of chars[i] will be a single char, not a string. You can't pass it into NSString stringWithUTFString: because it is expecting a nil-terminated C string, not a single character.

You should try something like this instead:

ichar = [NSString stringWithCharacters:&chars[i] length:1];

This error is most likely caused by this line:

ichar = [ NSString stringWithUTF8String:chars1[i] ];

NSString:stringWithUTF8String: requires a pointer to a string, and you are providing just one character to it.

The warning is telling you that you have passed an integer (a single char is an integer of sorts) in a pointer field; if you tried to run this code it would end poorly, as it will try to read invalid memory.

UTF-8 is a multi-byte encoding format; you can't iterate over it in this way. Consider placing the entire string inside an NSString and modify it using Cocoa's API, finally exporting the string you wish to use with GNUTLS back when done.

If you know that your string is UTF8, create an NSString object using – initWithUTF8String: then use – characterAtIndex: if you need to get individual characters.

Other methods are here:

http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFStrings/Articles/AccessingContents.html

Maybe this is what you want.

NSString *unicodeString = [NSString stringWithUTF8String:chars1]; 
for (int i=0; i < [unicodeString length]; i++) {
    NSString *ichar = [unicodeString substringWithRange:NSMakeRange(i, 1)];
    NSLog(@"Received %@", ichar);
    counter=strlen(chars1)-i;
    gnutls_record_send (session, [ichar UTF8String] , counter); 
}

That will take a string encoded in UTF-8 in chars1, and send the UTF-8 encoding of each individual character (which itself might be 1 or a few bytes) to gnutls_record_send.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM