如何在 If 语句的条件下使用 NSString 的值
How to use value of NSString in condition of an If statement
看似简单,却想不通。
我构建了一个具有多个评估标准的 NSString
。然后我想稍后使用 NSString
作为 if
语句中的条件。我如何使用 NSString
并对其进行评估?
目前,如果我在 if
语句中使用 NSString
,如果字符串存在,则为 TRUE
,而不是 TRUE
,如果 NSString
满足。
我正在根据一组特定标准更改地图注释的图像。 NSString
包含条件(内置于代码的其他部分)。我希望建立标准,然后在 viewForAnnotations 方法中执行标准。
感谢您的帮助。
if (annotString) {
haloImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@Full.png", [colorArray objectAtIndex:compareCounter]]];
compareHaloCounter++;
}
annotString 的值为:(annot.aState == 44) && (annot.aCounty == 20)
if(str isEqualToString:@"string") {
}
您可以使用 NSPredicate 来完成此操作。
@interface MyObject : NSObject
@property (nonatomic) NSUInteger propertyOne;
@property (nonatomic) NSUInteger propertyTwo;
@end
@implementation MyObject
@end
在一些实现代码中
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"propertyOne == 4 && propertyTwo == 5"];
MyObject *testObject = [[MyObject alloc] init];
testObject.propertyOne = 4;
testObject.propertyTwo = 5;
if ([predicate evaluateWithObject:testObject]) {
NSLog(@"predicate passed");
}
看似简单,却想不通。
我构建了一个具有多个评估标准的 NSString
。然后我想稍后使用 NSString
作为 if
语句中的条件。我如何使用 NSString
并对其进行评估?
目前,如果我在 if
语句中使用 NSString
,如果字符串存在,则为 TRUE
,而不是 TRUE
,如果 NSString
满足。
我正在根据一组特定标准更改地图注释的图像。 NSString
包含条件(内置于代码的其他部分)。我希望建立标准,然后在 viewForAnnotations 方法中执行标准。
感谢您的帮助。
if (annotString) {
haloImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@Full.png", [colorArray objectAtIndex:compareCounter]]];
compareHaloCounter++;
}
annotString 的值为:(annot.aState == 44) && (annot.aCounty == 20)
if(str isEqualToString:@"string") {
}
您可以使用 NSPredicate 来完成此操作。
@interface MyObject : NSObject
@property (nonatomic) NSUInteger propertyOne;
@property (nonatomic) NSUInteger propertyTwo;
@end
@implementation MyObject
@end
在一些实现代码中
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"propertyOne == 4 && propertyTwo == 5"];
MyObject *testObject = [[MyObject alloc] init];
testObject.propertyOne = 4;
testObject.propertyTwo = 5;
if ([predicate evaluateWithObject:testObject]) {
NSLog(@"predicate passed");
}