Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSString,匹配NSArray的名称_Objective C_Cocoa Touch - Fatal编程技术网

Objective c NSString,匹配NSArray的名称

Objective c NSString,匹配NSArray的名称,objective-c,cocoa-touch,Objective C,Cocoa Touch,我是C#NET的新目标C,我有这样一个场景: 假设我有5个NSArray对应5个UIButton。UIButton具有完全相同的名称 例如,一个UIButton被称为mainScreen,还有一个NSArray被称为mainScreen 这五个按钮链接到一个iAction,我在其中执行以下操作: - (IBAction)btnClick:(id)sender { NSString *category = [(UIButton *)sender currentTitle]; NSL

我是C#NET的新目标C,我有这样一个场景:

假设我有5个NSArray对应5个UIButton。UIButton具有完全相同的名称 例如,一个UIButton被称为mainScreen,还有一个NSArray被称为mainScreen

这五个按钮链接到一个iAction,我在其中执行以下操作:

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSLog(category);
   //Here I need to call the NSArray which has the same name as category
}

现在我可以得到UIButton的实际名称,但是如何才能得到与标题相同的NSArray?在不涉及大量if-else或switch语句的情况下,将名称与对象关联的最简单方法是使用
NSDictionary
(或者它的可变子类
NSMutableDictionary
)。字典将唯一键映射到对象。密钥可以是任何对象(实现
NSCopying
协议),但通常是
NSString
s

请看一下手册和指南


请注意,如果您使用按钮标题,如果您将应用本地化,则可能会中断。

我要做的是将数组存储在
NSDictionary
中。然后,您可以将“key”设置为数组的名称,然后该值就是数组本身

这样你可以说:

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSLog(category);
    //Here I need to call the NSArray which has the same name as category
    NSArray *theArray = (NSArray*)[self.myDictionary valueForKey:category];
}

希望这有帮助

你所做的不是最好的方法。您应该为每个按钮提供标签,比如从1到5。此外,还应将五个数组放入一个数组中。现在,您只需要:

- (IBAction)btnClick:(id)sender
{
  NSInteger index = [sender tag] - 1;

  NSArray *array = [bigArray objectAtIndex:index];
} 

就是这样。

为所有UIButton分配不同的标记,然后使用它们的标记显式访问它们

- (IBAction)btnClick:(id)sender {

    int tagIs  = [(UIButton *)sender tag];
    switch (tagIs) {
        case 1:
            // Access first button array
            break;
        case 2:
            // Access second button array

            break;
        default:
            break;
    }
}
或者可以使用AssociationObjects方法将数据与对象关联,如下所示: 首先,进口:

#import <objc/runtime.h>
  • ---其他键的方式相同---
然后使用:

// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects:@"object1",@"object 2", nil];
objc_setAssociatedObject((UIButton *)[self.view viewWithTag:1],
                         firstBtnKey,
                         firstArray,
                         OBJC_ASSOCIATION_RETAIN);

NSMutableArray *secondArray = [[NSMutableArray alloc] initWithObjects:@"object1",@"object 2", nil];


objc_setAssociatedObject((UIButton *)[self.view viewWithTag:2],
                         secondBtnKey,
                         secondArray,
                         OBJC_ASSOCIATION_RETAIN);
`

然后通过以下方式访问这些阵列:

- (IBAction)btnClick:(id)sender {

    int tagIs  = [(UIButton *)sender tag];
    switch (tagIs) {
        case 1:
            // Access first button array
    NSMutableArray *tempArr = (NSMutableArray *)objc_getAssociatedObject((UIButton *)sender, firstBtnKey);

            break;
        case 2:
            // Access second button array
    NSMutableArray *tempArr = (NSMutableArray *)objc_getAssociatedObject((UIButton *)sender, secondBtnKey);

            break;
        default:
            break;
    }
}

希望有帮助。

在大多数编程语言中,对象没有名称。仅仅因为UIButtons与NSArray(mainScreen)的名称完全相同,并不意味着您的对象“被称为”mainScreen。
使用
NSDictionary
,数组作为对象,按钮标题作为键。 或者使用按钮标签

title1= [[NSArray alloc] initWithObjects:@"1",nil];
title2= [[NSArray alloc] initWithObjects:@"2",nil];
title3= [[NSArray alloc] initWithObjects:@"3",nil];
title4= [[NSArray alloc] initWithObjects:@"4",nil];
title5= [[NSArray alloc] initWithObjects:@"5",nil];

dict = [[NSDictionary alloc] initWithObjectsAndKeys:title1,@"title1",title2,@"title2",title3,@"title3",title4,@"title4",title5,@"title5",nil];  

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSArray *arr = [dict objectForKey:category];

}

你最好使用标签,而不是stringsHow你是说“name”吗?数组代码中的实例变量名和按钮的标题文本?
title1= [[NSArray alloc] initWithObjects:@"1",nil];
title2= [[NSArray alloc] initWithObjects:@"2",nil];
title3= [[NSArray alloc] initWithObjects:@"3",nil];
title4= [[NSArray alloc] initWithObjects:@"4",nil];
title5= [[NSArray alloc] initWithObjects:@"5",nil];

dict = [[NSDictionary alloc] initWithObjectsAndKeys:title1,@"title1",title2,@"title2",title3,@"title3",title4,@"title4",title5,@"title5",nil];  

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSArray *arr = [dict objectForKey:category];

}