【问题标题】:Filling an NSMutableArray with int values and then retriveing them [duplicate]用 int 值填充 NSMutableArray,然后检索它们 [重复]
【发布时间】:2016-09-12 05:44:50
【问题描述】:

我正在将 BEMSimpleLineGraph 库集成到我的 Objective-C iPhone 应用程序中,我无法将核心数据中的 int 值添加到 NSMutableArray,然后在同一类中检索值,但使用不同的方法。

这是我用来填充数组的代码:

// Get those points from Core Data into my arrays
for (int i = 0; i < countInArray; i++) {

    NSManagedObject *device = [self.tapTestsConducted objectAtIndex:i];

    int leftInt, rightInt;

    leftInt = [[device valueForKey:@"leftNumber"] intValue];
    rightInt = [[device valueForKey:@"rightNumber"] intValue];

    [_leftArray addObject: [NSNumber numberWithInt:leftInt]];
    [_rightArray addObject: [NSNumber numberWithInt:rightInt]];

    NSDate *date = [device valueForKey:@"date"];
    [self.dateArray addObject: date];

    // For testing - Did the correct data get into the arrays?

    NSLog(@"%d", leftInt);
    NSLog(@"%d", rightInt);
    NSLog(@"%@", date);
}

这是用于查找图形 y 值的方法:

- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index {
NSNumber *anumber = [_leftArray objectAtIndex:index];
int anInt = [anumber intValue];

NSLog(@"%d", anInt);
return anInt;
}

第一部分的NSLog输出正确的数据,但是第二个方法的NSLog输出的都是0。

非常感谢任何建议或意见!

编辑:这是运行时的 NSLog 输出:

2016-09-11 22:30:04.072 CNS Test CD[16805:849994] 5 tests conducted
2016-09-11 22:30:04.072 CNS Test CD[16805:849994] 12
2016-09-11 22:30:04.072 CNS Test CD[16805:849994] 0
2016-09-11 22:30:04.072 CNS Test CD[16805:849994] 2016-08-31 23:49:12 +0000
2016-09-11 22:30:04.072 CNS Test CD[16805:849994] 20
2016-09-11 22:30:04.072 CNS Test CD[16805:849994] 0
2016-09-11 22:30:04.072 CNS Test CD[16805:849994] 2016-09-02 23:31:44 +0000
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 0
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 17
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 2016-09-02 23:32:12 +0000
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 12
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 0
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 2016-09-05 19:13:19 +0000
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 13
2016-09-11 22:30:04.073 CNS Test CD[16805:849994] 0
2016-09-11 22:30:04.074 CNS Test CD[16805:849994] 2016-09-05 19:13:55 +0000
2016-09-11 22:30:05.949 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.950 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.951 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.955 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.955 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.955 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.956 CNS Test CD[16805:849994] 0
2016-09-11 22:30:05.956 CNS Test CD[16805:849994] 0

【问题讨论】:

  • 在 for 循环之后打印dateArray 的输出是什么?或者您是否收到任何错误,如果是,请也添加错误。
  • _leftArray 在每一点上都不是零吗?
  • 您的leftArray 是否已初始化为NSMutableArray?确保在您操作之前对其进行初始化。
  • 感谢大家的帮助!我根据您的想法进行了一些调试,并找到了解决方案。菜鸟错误... :)

标签: ios objective-c iphone nsmutablearray bemsimplelinegraph


【解决方案1】:

我已经在我的 .h 文件中正确声明了我的 NSMutableArrays:

@property (strong, nonatomic) NSMutableArray *leftArray;
@property (strong, nonatomic) NSMutableArray *rightArray;
@property (strong, nonatomic) NSMutableArray *dateArray;

我没有做的,在我做之后解决了这个问题,就是用 alloc init 初始化我的 arras。我将这些行放在 for 循环之前。

self.leftArray = [[NSMutableArray alloc] init];
self.rightArray = [[NSMutableArray alloc] init];
self.dateArray = [[NSMutableArray alloc] init];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多