简体   繁体   中英

NSMenuItem Title Set to String and NSString?

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.xx

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

Try:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM