user1809923
user1809923

Reputation: 1255

Converting NSString* to char* with ARC

I am trying to do the following set up an udp connection in an Iphone App like this:

 struct sockaddr_in server_address;
 CFDataRef server_address_data;
  ....      


  memset(&server_address, 0, sizeof(server_address));
  server_address.sin_len = sizeof(server_address);
  server_address.sin_family = AF_INET;
  server_address.sin_port = htons(thePort);      
  inet_aton([theHost cStringUsingEncoding:[NSString UTF8String]], &server_address.sin_addr);
  // put server address into CFData
  server_address_data = CFDataCreate(NULL, (uint8_t *) &server_address, sizeof(server_address));
  // set timeout (ACK and RESULT packets) for socket
  ????

Now, I have the following problem: the variable theHost comes from a TextField and is of type NSString*. I want to get the ip address from it and hence try inet_aton, which needs a char *. How do I transform it (with using ARC)? My try above fails, because there is "No known class method for selector UTF8String". Also I want to set a timeout for my socket for when we receive..

Upvotes: 0

Views: 749

Answers (3)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21996

You wrote [NSString UTF8String], but UTF8String isn't a static method, so you have first to create an instance of the string, you can replace that line with:

inet_aton([theHost cStringUsingEncoding:[@"String that you want" UTF8String]], &server_address.sin_addr);

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122458

Just use [NSString UTF8String] to get the UTF-8 encoded string:

inet_aton([theHost UTF8String], &server_address.sin_addr);

Upvotes: 2

Mike Weller
Mike Weller

Reputation: 45598

-UTF8String is an instance method, so you need to use [theHost UTF8String] which will return a char *.

Upvotes: 2

Related Questions