views:

43

answers:

1

I currently add objects to an array with a label taken from the textfield input from the user.

At the moment it adds an entry even if the field is blank.

I assign the textfield to a string and then want to check if it is nil, if it is then dont add it to the array.

I am used to java where it could be done with something like

if(enteredText.length > 0){ //add to array}

I am trying the following in my code now

if(title != nil)
{
[plistArray addObject:title];
[plistArray writeToFile:filepath atomically: YES];
}

but it doesnt seem to work and there are no dot methods available to get the length of the entered text.

How could I achieve this in Obj-C?

Regards

+3  A: 

Use the -length method of NSString

if(title != nil && [title length])
{
    [plistArray addObject:title];
    [plistArray writeToFile:filepath atomically: YES];
}
fsmc
great, thanks. So instead of the dot in java, in objective c just add [ ] and lose the dot?
alJaree
@alJaree: No, the obj-c dot notation is used for specific kinds of method calls. My advice: don't come to some "instead of X do X" conclusion, instead actually learn what the syntax means.
Dan Ray
@Dan Ray : I know about the dot notation in obj-c, but I should have re stated my comment.I was thinking instead of in java where you can use the dot notation and then see the available methods and learn at the same time, could the same be done in obj-c by [object codesense options]?regards
alJaree
if ([title length]) { ... } would actually be enough here, no?
Eiko
yeah, seems to be enough. thanks. :)
alJaree
@alJaree: Oh, sure! If you start a bracketed message clause, you can go [object and hit escape to see completion options.
Dan Ray
@Dan Ray : I am really new to objective C and read that the dot operator was introduced in 2.0. Will this become more used in obj c or is it better to stick to the standard [ ] technique.
alJaree
@alJaree: It's not either-or. They're used for different things. Or, more accurately, dot notation is a shortcut for certain, specific things. If you're setting up synthesized properties, you can access their getters and setters via dot notation. That's all. It's not for calling other methods. It lets you think of those properties as data fields of the object. That's the difference. You'll use them both a lot, and you're going to want to know when and why.
Dan Ray
@Dan Ray : Thanks. synthesized properties are great, saves loads of time. I have only been trying objective C for a week now, personally I am already liking it more than java :P
alJaree