Re: Number in NSString to int.
Re: Number in NSString to int.
- Subject: Re: Number in NSString to int.
- From: "I. Savant" <email@hidden>
- Date: Fri, 31 Oct 2008 16:58:58 -0400
On Fri, Oct 31, 2008 at 4:38 PM, Adam Penny <email@hidden> wrote:
> My main question is how to get an int from an NSString,
See NSScanner.
> NSArray *macArray=[[NSArray alloc] init];
> macArray=[mac componentsSeparatedByString:@":"];
> for (int i=0; i < [macArray count] ; i++)
> {
> //somehow need to get an int called dec from the number in the
> NSString then...
> NSString *hex=stringWithFormat:@"%x",dec;
> [macArray[i] autorelease];
> macArray[i]=hex;
>
> }
You have a lot of reading to do. Look for the introductory
Objective-C material on Apple's site and/or pick up a good book on the
subject. This is almost all gibberish, unfortunately.
> NSArray *macArray=[[NSArray alloc] init];
You've created a new immutable array. It is empty and, because it's
immutable, it's nearly useless (unless you intend to send an empty
array to some other object - in which case you've leaked it if you
don't release it).
> macArray=[mac componentsSeparatedByString:@":"];
You've leaked the array you just created by assigning a new array to
macArray. Incidentally, if you look at the documentation for
-[NSString componentsSeparatedByString:], you'll see that it also
gives you an immutable array. This is important for the line below ...
more on that in a bit. Note also that, if you read the memory
management documentation carefully, you didn't directly create this
array, so it is autoreleased.
> for (int i=0; i < [macArray count] ; i++)
This is fine if you're using C99 mode ... otherwise, you'll need to do this:
int i;
for (i=0; i < [macArray count] ; i++)
... or you'll get a complier error (you can't declare a variable
inside the for() initializer.
> //somehow need to get an int called dec from the number in the
> NSString then...
As above, see NSScanner and its associated documentation.
> NSString *hex=stringWithFormat:@"%x",dec;
Utter gibberish, sayeth the compiler. To what object or class are
you sending the "stringWithFormat:" message? Also, are you sure you
want an unsigned integer (%x) in your string format? Are you sure you
don't just want an integer (%i)?
> [macArray[i] autorelease];
> macArray[i]=hex;
Also gibberish. First, NSArray is an object, not a C array. Read up
on NSArray's documentation to discover how to access elements (hint:
-objectAtIndex:). Second, as referenced above, macArray is an
immutable array. You'll want to create (and autorelease) a
-mutableCopy of the "componentsSeparatedByString:" if you want to
mutate the array. Then you want to "replace" an "object at" a given
"index" (another hint). Third, why are you individually autoreleasing
objects in an array? Did you create them? Nope. Leave 'em alone.
As I said, you have a lot of reading to do, as this code will
probably anger your complier quite efficiently. There are a lot of
concepts you appear to be missing, so the best investment you can make
right now is "study time". A word of encouragement: it *will* make
sense if you keep at it. Good luck!
--
I.S.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden