-2

Hey i'm new in xcode coding so i'm learning my self.

I write this code and an errors appear point to the first of "|" saying EXPECTED EXPRESSION BEFORE'||' TOKEN. this is my if statement:

if ((nametextfield.text=@"")) || ([secretPin isEqual:null])

And null is wright?

SO any help?

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Ram
  • 11
  • 1
  • 3

3 Answers3

2

A few problems:

  • In C (and Objective-C) a single equal sign as you have used is for assignment, not comparison
  • Secondly, the proper way to do string comparison in Objective-C is to use the NSString method isEqualToString:
  • Objective-C uses nil instead of null
  • In Objective-C a pointer is nil it will evaluate to false, so instead of using isEqual you can just test !secretPin (which will return true is secretPin is nil)
  • As others have pointed out, your parentheses are incorrectly set in your conditional

So a more correct rewrite is:

if ( [self.nametextfield.text isEqualToString:@""] || !secretPin ) {
    // do something
}

EDIT:

Also note that I have edited this example so that nametextfield is now a property of self (where self in this case is your View Controller). The "unexpected identifier" error makes me think you have not connected your Text Field object (created in your storyboard or xib file) to your view controller correctly. You should be sure you are declaring the text field as a property of your view controller like this in your header (.h) file:

@property (nonatomic, weak) IBOutlet UITextField* nametextfield;

and synthesize the property in your implementation (.m) file:

@synthesize nametextfield;

and then attach the UITextField in your storyboard or xib to this IBOutlet.

jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • Jonkroll i appreciate your help. But my nametextfield is not a NSString is a UItextfield ,So i use nametextfield.text=@"" and 3 errors appear: 1-Expected identifier. 2-confused by earlier errors, bailing out. 3-expected ':' before "]" token. – Ram Feb 26 '12 at 21:16
  • jonkroll i create a variable named Empty of type NSString and i give'it nametextfield.text so my code run correctly. NSString *Empty=nametextfield.text; if ( [ Empty isEqualToString:@"" ] || !secretPin){ – Ram Feb 26 '12 at 22:06
  • Oops, you are correct, I missed the ".text" property in my code example. Also, your "Expected identifier" error makes me think you have not connected the UITextField to your View Controller correctly. See my edit to my answer above. – jonkroll Feb 26 '12 at 22:45
  • Hey man yes i did my declaration and write my synthesize(working like get and set)and i attached the UItextfield and my apps run successfully. My apps is so simple every developer write it when he starting programming in any language.my apps talk about that i have to enter my name and my pin(name and pin are textfield) and then click on a button named click me(plus i have a button named clear pin) and an alert view came out and saying hello and show my name. – Ram Feb 27 '12 at 01:20
  • I'm a Junior developer from lebanon and here there is no university learn mobile apps so i am learning my self and i have a lot lot lot of questions(i'm making a lot of research),it's a very big fields and i love to work in it and i hope after a 6 7 years to be a very good programmer. Anyway in your reply you tell me about NSTextfield so whats is the different between NSTextfield and UItextfield? – Ram Feb 27 '12 at 01:21
  • Oops again, I meant UITextField. I was typing too fast. There is no such thing as NSTextField. – jonkroll Feb 27 '12 at 03:12
0

I presume this is Objective-C?? If so, you've got a misplaced paren:

if ((nametextfield.text=@"") || ([secretPin isEqual:null]))

But also, nametextfield.text=@"" is wrong on two counts, and the standard Objective-C null pointer value is nil.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

You don't even need the parens for the expressions. It should be

if ( nametextfield.text = @"" || [secretPin isEqual:null] )

That compiles for me and runs with dummy data.

rinzler
  • 236
  • 1
  • 4