简体   繁体   English

ios-数组中的NSNumber

[英]NSNumber in arrays, ios

I am trying to learn about how to put numbers into an array with nsnumber. 我正在尝试学习如何将数字放入nsnumber数组中。 The exact thing I'm stuck with is, To build the sequence in the array, we're going to need a loop. 我遇到的确切问题是,要在数组中构建序列,我们需要一个循环。 Between creating the sequence array and returning it, declare a for loop whose counter is limited by index + 1 and increments by one. 在创建序列数组并将其返回之前,声明一个for循环,其计数器受索引+ 1限制,并递增1。 Since the sequence requires the two previous numbers to calculate the next one, we need to prime the sequence. 由于该序列需要前两个数字来计算下一个,因此我们需要对序列进行预填。 We're going to need to manually pass in @0 and @1 on the first two iterations of the loop. 我们将需要在循环的前两次迭代中手动传入@ 0和@ 1。 This is what I have so far. 到目前为止,这就是我所拥有的。

(NSArray *)arrayWithFibonacciSequenceToIndex:(NSUInteger)index
{
NSMutableArray *sequence = [NSMutableArray array];

for(NSUInteger i = 0; i < 1; i++)
{
    index = i+1;

}

return sequence;
}

Am I on the right track? 我在正确的轨道上吗? I'm not sure if my for loop is correct. 我不确定我的for循环是否正确。 Do I put sequence into the for loop and add the nsnumber @0 and @1 there or do I put those numbers into the sequence outside the loop? 我应该将序列放入for循环中并在其中添加nsnumber @ 0和@ 1还是将这些数字放入循环外的序列中?

To insert a number in an NSArray, you have to wrap them in a NSNumber: 要在NSArray中插入数字,必须将它们包装在NSNumber中:

NSInteger a = 5;
NSNumber number = @(a); // ou @5;

to perform mathematical operations on 2 NSNumbers, you have to convert them to integer (or double, float...) before 要对2个NSNumber执行数学运算,必须先将它们转换为整数(或double,float ...)

NSNumber * number1 = @1;
NSNumber * number2 = @6;
NSInteger sum = [number1 integerValue] + [number2 integerValue];

for the fib problem, youre loop is correct. 对于fib问题,您的循环是正确的。 The way I would think of this is : I add my value in the for loop, and if I'm adding the 1st or 2nd element, then I put a 0, else I sum the last 2 elements: 我的想法是:我在for循环中添加我的值,并且如果我添加第1个或第2个元素,则放置0,否则我将最后2个元素相加:

- (NSArray *) fibbonacciSequenceWithSize:(NSInteger)size
{
    NSMutableArray * result = [NSMutableArray new];

    for(NSInteger idx = 0; i < size ; i ++)
    {
        // first 2 numbers of fib sequence are 1
        if(idx == 0 || idx == 1)
        {
             [result addObject:@1];
        }
        else
        {
            // Add the 2 previous number
            // F2 = F1 + F0
            NSinteger next = [result[idx - 2] integerValue] + [result[idx - 1] integerValue];

            [result addObject:@(next)];
        }
    }
    return [result copy]; // copy the NSMutableArray in a NSArray
}

You can clean up the code by having a Fibonacci function that provides the sum of the last two elements. 您可以通过提供最后两个元素之和的Fibonacci函数来清理代码。

- (NSNumber *)nextFibInArray:(NSArray *)array {
    if (array.count < 2) return @1;
    NSInteger lastIndex = array.count - 1;
    return @([array[lastIndex-1] intValue] + [array[lastIndex] intValue]);
}

Then the loop is cleaner, too. 然后,循环也更干净了。

- (NSArray *)fibonacciWithLength:(NSInteger)length {
    NSMutableArray *result = [@[] mutableCopy];
    for (NSInteger i=0; i<length; i++) {
        [result addObject:[self nextFibInArray:result]];
    }
    return result;
}

We could trim some execution time fat from this, but for short enough sequences, this should be clear and quick enough. 我们可以从中节省一些执行时间,但是对于足够短的序列,这应该足够清楚和快速。

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

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