I suddenly just thought of this and got stuck deciding which data type should I use to store an IP Address?
I have thought of NSString; But if I would need the last digit for identifications, should I use float or double? And that is also another problem, since when can float or double have more than 1 decimal point?
I am probably asking the question wrongly, because I really don’t know how to ask this.
The IP Address comes from an XML format <IP>192.168.1.1</IP>. Any idea how I should do this?
Use
NSString. You are not going to do arithmetic with it. If you need the separate components you can useNSArray, butNSStringwill serve you well for just storing the IP address.In response to your needs, you can always obtain the last character in your string using the
NSStringmethod:Where ip_string is the string holding the IP address.
Edit in response to comment:
Logan’s code is storing each element in the IP address separated by a period into an array. So if the IP address is 192.168.1.1, the array will equal (192, 168, 1, 1).
My code is storing the entire IP address in a string, and then obtaining the last character in that string.
[ip_string substringFromIndex: [ip_string length] - 1]is just obtaining the last character in the string containing the IP address. The last character can be found at minus one character.So if the IP address is 192.168.1.1, the
lastCharacterstring will just contain the number1.I suggested that code because you stated that you needed to do something with the last character in your IP address string, and my code shows how you can obtain the last character.