将BOOL列表传递给objective-c中的方法的最短路径?

时间:2021-01-31 22:47:00

What's the shortest way to pass a list of BOOL's to a method in objective-c? (i.e. without having to set up entries in an NSArray using NSNumber to wrap them).

将BOOL列表传递给objective-c中的方法的最短路径是什么? (即,不必使用NSNumber在NSArray中设置条目来包装它们)。

Background: I want to call a test method many times in a unit test so want a shorthand way to effectively say:

背景:我想在单元测试中多次调用一个测试方法,所以想要一个简便的方法来有效地说:

[myCustomObject compareWithBools:[TRUE, FALSE, TRUE, TRUE, TRUE]]

In the receiving method "compareWithBools" I can put in the lines of code to massage back to be able to compare, however just wanted a nice short line within the tests I can call...

在接收方法“compareWithBools”中我可以放入代码行来按摩以便能够进行比较,但只是想在我可以调用的测试中找到一条很好的短线...

3 个解决方案

#1


1  

Skip the NSNumber wrapper with a macro?

使用宏跳过NSNumber包装器?

#define Greg_BOOLFromNSString(s) ([(s) isEqualToString:@"YES"])

[myCustomObject compareWithStrings:[NSArray arrayWithObjects:@"YES", @"NO", @"YES", @"YES", @"YES", nil]];

#2


4  

this approach is fairly easy

这种方法相当容易

- (NSComparisonResult)compareWithBools:(BOOL*)bools /* << a pointer to an array of BOOLs, which is owned by the caller */
                                 count:(NSUInteger)count; /* << the number of elements in @a bools */

#3


2  

Pass an integer and compare separate bits of that integer:

传递一个整数并比较该整数的不同位:

[myCustomObject compareWithBools:13]; // which will be a sequence of 1011

#1


1  

Skip the NSNumber wrapper with a macro?

使用宏跳过NSNumber包装器?

#define Greg_BOOLFromNSString(s) ([(s) isEqualToString:@"YES"])

[myCustomObject compareWithStrings:[NSArray arrayWithObjects:@"YES", @"NO", @"YES", @"YES", @"YES", nil]];

#2


4  

this approach is fairly easy

这种方法相当容易

- (NSComparisonResult)compareWithBools:(BOOL*)bools /* << a pointer to an array of BOOLs, which is owned by the caller */
                                 count:(NSUInteger)count; /* << the number of elements in @a bools */

#3


2  

Pass an integer and compare separate bits of that integer:

传递一个整数并比较该整数的不同位:

[myCustomObject compareWithBools:13]; // which will be a sequence of 1011