【问题标题】:how to compare array of elements with string in ios? [closed]如何在ios中将元素数组与字符串进行比较? [关闭]
【发布时间】:2012-09-10 05:18:13
【问题描述】:
    - (void)viewDidLoad
    {
        [super viewDidLoad];

 NSMutableArray *imageArray=[[NSMutableArray alloc]initWithObjects:@"pic1.png",@"pic2.png",@"pic3.png",@"pic4.png",nil];


NSMutableArray *username=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D" ,nil];

        [self scrollViewAction];

    }




    -(void)scrollViewAction

    {

        int x=5,y=0;

        for(int i=0;i<[DetailsArray count];i++)

        {

            UIButton *btn=[[UIButton alloc]init];


[btn setFrame:CGRectMake(x, y, 90, 90)];

            UILabel *usernameLabel=[[UILabel alloc]init];

            [btn addTarget:self action:@selector(btnClicked) 

forControlEvents:UIControlEventTouchUpInside];

            btn.tag=i;

            image=[UIImage imageWithData:[NSData dataWithContentsOfFile:@"imageArray"]];

            if(i==0||i==1||i==2)


            {


                usernameLabel=[[UILabel alloc]initWithFrame:CGRectMake(x,90,90,10)];


            }

else

            {


                usernameLabel=[[UILabel alloc]initWithFrame:CGRectMake(x,y+90,90,10)];

            }



            usernameLabel.text=username;

    -------------------------------------------------------(here I am getting cant compare 

array with string)

            usernameLabel.backgroundColor=[UIColor blueColor];

            usernameLabel.font=[UIFont systemFontOfSize:12];

            [usernameLabel setTextAlignment:UITextAlignmentCenter];

            [self.scrollview addSubview:btn];

            [self.scrollview addSubview:usernameLabel];

             x=x+110;

            if((i+1)%3==0)

            {

                x=5;


                y=y+110;//y=y+115;




            }



            self.scrollview.contentSize= CGSizeMake(320,y+200);

        }




    }

【问题讨论】:

  • 我真的不确定您在这里要做什么,您要使用什么数组以及为什么要将数组与字符串进行比较?您的意思是要在字符串数组中找到匹配的字符串吗?
  • 是的...我想在字符串数组中找到匹配的字符串
  • 如果您希望人们阅读代码并给出答案并提供一些关于您想要什么的解释,您应该正确地格式化您的代码,而不仅仅是格式非常糟糕的代码转储。
  • 匹配什么?我在这段代码中没有看到任何匹配任何内容的尝试。
  • MAI 只看到代码,问题出在哪里?

标签: iphone ios xcode cocoa


【解决方案1】:
usernameLabel.text=username;

username 是 NSMutableArray 的一个实例,这不是预期的(NSString 的一个实例)。 也许你想做这样的事情?

usernameLabel.text=[username objectAtIndex:0];

【讨论】:

    【解决方案2】:
    for (NSString *name in username) {
       if ([name isEqualToString(usernameLabel.text)])
    //matched string found
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 2020-04-06
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2019-02-22
      相关资源
      最近更新 更多