简体   繁体   English

比较iPhone中的两个NSMutableArray索引

[英]Compare two NSMutableArray indices in iPhone

I am new to iPhone develepment, 我是iPhone开发的新手,

I want to compare two array indices, 我想比较两个数组索引,

for (int i=0; i<[CustomeDateArray count]; i++)
  {
      if (([CustomeDateArray objectAtIndex:1] == [newDateArray objectAtIndex:1]) && ([CustomeDateArray objectAtIndex:2] == [newDateArray objectAtIndex:2]))          
                {
                    exists=TRUE;
                    NSLog(@"exists=TRUE");
                }
  }

My Log shows this Results: 我的日志显示以下结果:

 CustomeDateArray at Index1=06
 CustomeDateArray at Index2=2012

 newDateArray at Index1=06
 newDateArray at Index2=2012

If my if condition is true then control should go inside and it should print exists=TRUE but i am unable to see exists=TRUE control is not going inside. 如果我的if条件为true,则控件应该放在里面,并且应该打印exists=TRUE但是我看不到exists=TRUE控件不在里面。

What's the problem ? 有什么问题 ?

Any help will be appreciated. 任何帮助将不胜感激。

for (int i=0; i<[CustomeDateArray count]; i++)
  {
          if (([[CustomeDateArray objectAtIndex:1] isEqual:[newDateArray objectAtIndex:1]]) && ([[CustomeDateArray objectAtIndex:2] isEqual:[newDateArray objectAtIndex:2]]))        
                {
                    exists=TRUE;
                    NSLog(@"exists=TRUE");
                }
  }

This May Helping to you Happy Coding:-) 这可能会帮助您快乐编码:-)

Well, I see to kind of problems in your code: 好吧,我发现您的代码中存在一些问题:

  1. why you are looping inside CustomDateArray (for loop) and you are not using the index "i"? 为什么在CustomDateArray(用于循环)内部循环,而没有使用索引“ i”? (this is not relevant to the specific question, but just check your code for typos!) (这与特定问题无关,但只需检查您的代码是否有错字!)

  2. more specific to your question: NSArray contains objects and objects in Obj-C are pointers, so your "==" just compares pointers. 更具体地针对您的问题:NSArray包含对象,而Obj-C中的对象是指针,因此您的==只是比较指针。 This means that: 这意味着:

if([CustomDateArray objectAtIndex:1]==[CustomDateArray objectAtIndex:2]) ...

corresponds to: 对应于:

id obj1 = [CustomDateArray objectAtIndex:1];
id obj2 = [CustomDateArray objectAtIndex:2];
if(obj1==obj2) ...

The "if" will return true only if obj1 and obj2 point to the same address, so they are exactly the same object. 仅当obj1和obj2指向相同的地址时,“ if”才返回true,因此它们是完全相同的对象。 But if the purpose of your check is to know if the two dates are the same date, then you should use the NSDate specific comparison methods: 但是,如果检查的目的是要知道两个日期是否相同,则应使用NSDate特定的比较方法:


NSDate *d1 = [CustomDateArray objectAtIndex:1];
NSDate *d2 = [CustomDateArray objectAtIndex:2];
if([d1 isEqualToDate:d2]) ...

Instead, if the objects are string, you should use the "isEqualToString:" method. 相反,如果对象是字符串,则应使用“ isEqualToString:”方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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