0

So basically I have an IBoutlet NSMenuItem called localIP. I am using the setTitle instance in my .m and I want to set the title equal to a string (@"") and an NSString.

Look at the following fragment.

[localIP setTitle:(@"Local IP: %@", ip)];

The problem is I get a warning saying expression result unused. So what is displayed is the value of the NSString ip. I want the final output to be like: Local IP: 192.xxx.x.x

I am new to objective-c. I primarily programmed in java before.

Andrew Sheridan
  • 19
  • 1
  • 10

2 Answers2

2

Try:

NSString * title = [NSString stringWithFormat:@"Local IP: %@",ip]; 
[localIP setTitle:title];

Also, ip is instance of NSString in your program, right? :)

Ondrej Peterka
  • 3,349
  • 4
  • 35
  • 49
  • Thank you very much! Worked! Do you mind explaining why though? What makes stringWithFormat different than NSString by itself? – Andrew Sheridan Apr 27 '12 at 04:27
  • What you originally had was a use of the C comma operator. The comma operator evaluates the expression to the left of the comma, *throws away its value*, evaluates the expression to the right of the comma, and takes its value as the value of the overall expression. The value of this is that the left side may have had side effects (e.g. an assignment). – Ken Thomases Apr 27 '12 at 04:30
  • Thanks I think I understand that better! – Andrew Sheridan Apr 27 '12 at 04:31
0

You'll need to use the following:

[localIP setTitle:[NSString stringWithFormat:@"Local IP: %@", ip]];

As far as I'm aware there's currently no way to do this just using literals.

The warning occurs, as the second expression in the brackets, the 'ip' variable is discarded by the compiler, hence 'expression unused'.

There's a similar question here

Community
  • 1
  • 1
Stuart Ervine
  • 1,024
  • 13
  • 15