• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 2 个按钮操作中的 1 个类中的可变数组

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:11:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我通过 1 个 IBAction 生成了一些不可变数组,但是当我从同一个类中的另一个 IBAction 引用它们时,我无法检索答案。我在第一个 Action 中创建的部分代码是:

所有数组都在第一个 IBAction 中初始化。这是 vType 的示例:

NSMutableArray *vType = [[NSMutableArray alloc] init];

// Gather components for Value Calculations  

//NSLog(@" myInv count(h) = %lu and catDetail count(g) = %lu", (unsigned long)[myInv count], (unsigned long)[catDetail count]);

for (h = 0; h < [myInv count]; h++){

    //NSLog(@"vScott objectAtIndex:h = %@ AT %d", [vScott objectAtIndex:h], h);

    for (g = 0; g < [catDetail count]; g++){

        //NSLog(@"cvScott objectAtIndex:g = %@ AT %d", [cvScott objectAtIndex:g], g);

        //NSLog(@"vScott objectAtIndex:h = %@ AT %d", [vScott objectAtIndex:h], h);

        if ([[vScott objectAtIndex:h] isEqualToString:[cvScott objectAtIndex:g]]){

            [vSerStart insertObject:[cvSerStart objectAtIndex:g] atIndex:h];
            [vType insertObject:[cvType objectAtIndex:g] atIndex:h];
            [vUnitCost insertObject:[cvFacePrice objectAtIndex:g] atIndex:h];
            [vYear insertObject:[cvYear objectAtIndex:g] atIndex:h];

            //NSLog(@"vScott (vIndex) / cvScott (cvIndex) = %@ (%d) / %@ (%d)", [vScott objectAtIndex:h], h,[cvScott objectAtIndex:g],g);
            //NSLog(@"cvSerStart Object %@ atIndex:p = %d",[cvSerStart objectAtIndex:g], g);
            //NSLog(@"cvType Object %@ atIndex:p = %d",[cvType objectAtIndex:g], g);
            //NSLog(@"vScott / vSerStart / vType // AT (myInv) Index p =  %@ / %@ / %@ // %d", [vScott objectAtIndex:h],[vSerStart objectAtIndex:h], [vType objectAtIndex:h],h);
            //NSLog(@"+++++++++++++++++++++++++++++++++++++++++");


            //NSLog(@" The value calculation Scott Numbers are:%@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@ * %@",[vScott objectAtIndex:h], [vExt objectAtIndex:h], [vSer objectAtIndex:h], [vNew objectAtIndex:h], [vUsed objectAtIndex:h], [vPlate objectAtIndex:h], [vSht objectAtIndex:h], [vSerPrice objectAtIndex:h], [vNewPrice objectAtIndex:h], [vUsedPrice objectAtIndex:h], [vPBPrice objectAtIndex:h], [vShtPrice objectAtIndex:h], [vSerStart objectAtIndex:h],[vType objectAtIndex:h]);

            vOutLine = [NSString stringWithFormat"%@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@", [vScott objectAtIndex:h], [vYear objectAtIndex:h], [vExt objectAtIndex:h], [vType objectAtIndex:h], [vSer objectAtIndex:h],[vNew objectAtIndex:h], [vUsed objectAtIndex:h], [vPlate objectAtIndex:h], [vSht objectAtIndex:h], [vUnitCost objectAtIndex:h], [vSerPrice objectAtIndex:h], [vNewPrice objectAtIndex:h], [vUsedPrice objectAtIndex:h], [vPBPrice objectAtIndex:h], [vShtPrice objectAtIndex:h], [vSerStart objectAtIndex:h]];

    //NSLog(@"vOutLine = %@", vOutLine);
    NSLog(@"%@",[vType objectAtIndex:h]);

    [valueData addObject:vOutLine];

可变数组 (vType) 的结果是:

2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C
2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C
2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C
2014-10-24 23:00:53.233 Stamp Collection[6313:1331161] C

尝试使用存储在同一类中的第二个操作中的值,我正在使用此代码进行测试:

- (IBAction)btnValueCollectionDataTableid)sender {

    int k,l,m,n;
    NSMutableArray *vType;

    //if ([[vScott objectAtIndex:h] isEqualToString:[cvScott objectAtIndex:g]]

    for (k = 0; k < [myInv count]; k++){

        NSLog(@"%@",[vType objectAtIndex:k]);
        //if ([[vType objectAtIndex:k] isEqualToString"C"]){

           //convert required elements to Integer


           //calculate required elements

    //}else{

    }
}

这个vType数组的试验结果是:

2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)
2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)
2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)
2014-10-24 23:01:00.634 Stamp Collection[6313:1331161] (null)

我认为一旦在类中初始化一个数组就可以在任何地方使用。我会很感激建议,因为我被困在这里。



Best Answer-推荐答案


NSMutableArrays 被声明为每个方法的本地。如果您想从多个方法访问相同的 NSMutableArray ,请将其作为类的属性(或实例变量)。

您需要阅读变量作用域的基础知识。

关于ios - 2 个按钮操作中的 1 个类中的可变数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26559120/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap