相关疑难解决方法(0)

检查Objective-C中的相等性

如何检查字典中的键与方法参数中的字符串相同?即在下面的代码中,dictobj是NSMutableDictionary的对象,而对于dictobj中的每个键,我需要与string进行比较.怎么做到这一点?我应该指定NSString的键吗?

-(void)CheckKeyWithString:(NSString *)string
{
   //foreach key in NSMutableDictionary
   for(id key in dictobj)
     {
       //Check if key is equal to string
       if(key == string)// this is wrong since key is of type id and string is of NSString,Control doesn't come into this line
          {
           //do some operation
          }
     }
}
Run Code Online (Sandbox Code Playgroud)

objective-c iequalitycomparer

13
推荐指数
1
解决办法
2万
查看次数

如何在Objective-C中测试相等性?

比较顶视图时,第一个{}无法执行.==平等测试失败.

init,我

    [self setCurrentPuzzleView:p1];
Run Code Online (Sandbox Code Playgroud)

后来,我

if ([self currentPuzzleView] == p1) {
    NSLog(@"Removing P1 from SuperView");
    [p1 removeFromSuperview];
} else {
    NSLog(@"Removing P2 from SuperView");
    [p2 removeFromSuperview];
}
Run Code Online (Sandbox Code Playgroud)

这是现在应该如何比较观点?我认为==对于指针类型是可以的

if ([self currentPuzzleView] == p1) {
Run Code Online (Sandbox Code Playgroud)

视图之间的关系通过InterfaceBuilder建立,其中每个视图(p1,p2等)都声明为IBOutlet Puzzle1 *p1.

objective-c

3
推荐指数
1
解决办法
826
查看次数

标签 统计

objective-c ×2

iequalitycomparer ×1