Initialize NSArray with floats?

后端 未结 7 2121
你的背包
你的背包 2021-02-12 19:54

Is this a valid way to initalize an NSArray with float objects?

NSArray *fatArray = [NSArray arrayWithObjects:
                    [NSNumber numberWithFloat:6.9]         


        
7条回答
  •  北荒
    北荒 (楼主)
    2021-02-12 20:47

    If you're going to be creating a lot of these arrays with exactly the same number of elements, you could define a FloatArrayFactory class that implements something like:

    + (NSArray *) arrayWithFloat: (float)f;
    + (NSArray *) arrayWithFloat: (float)f1 and: (float)f2;
    + (NSArray *) arrayWithFloat: (float)f1 and: (float)f2 and: (float)f3;
    + (NSArray *) arrayWithFloat: (float)f1 and: (float)f2 and: (float)f3 and: (float)f4;
    

    And if that's too verbose, you could go for the terse and funky look of unnamed parameters:

    + (NSArray *) arr: (float)f1 : (float)f2 : (float)f3;
    

    which you can call like this:

    NSArray *a = [Factory arr: 1.0 :2.0 :3.0 :4.0];
    

提交回复
热议问题