How do you wrap up a BOOL for KVC in Cocoa/Obj-C?
Asked Answered
P

2

10

I'm using KVC to iterating through a few views. Having trouble setting BOOL properties:

[self setValue:YES forKeyPath:[NSString stringWithFormat:@"myView%d.userInteractionEnabled", n]];

I get: warning: passing argument 1 of 'setValue:forKeyPath:' makes pointer from integer without a cast.

There is no [NSValue valueWithBool:YES] or similar that I can find.

What to do?

Postfix answered 2/7, 2009 at 22:21 Comment(0)
A
30

The compiler is generating a warning because the first argument of -setValue:forKeyPath: expects and object. YES is not an object.

The answer is right there in "NSValue.h":

[NSNumber numberWithBool: aBool]

Later versions of Xcode allow you to use the literal syntax:

[foo setValue:@YES forKey:@"bar"]

Arvind answered 2/7, 2009 at 22:28 Comment(0)
A
1

You also can use Literal expression @(YES)

For More Info llvm Objective-C Literals

Anna answered 20/8, 2013 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.