Initialize NSArray with floats?

后端 未结 7 2107
你的背包
你的背包 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:29

    If you have a lot of floats to convert, or want to paste them in from a comma separated list, it may be easier to call a function that converts floats into an NSArray instance.

    NSArray*  arrayFromFloats(NSUInteger count, float *floats)
    {
        NSMutableArray* ma = [NSMutableArray arrayWithCapacity:count]; 
        NSUInteger i; 
        for (i=0; i<count; i++) { 
            [ma addObject:[NSNumber numberWithFloat:floats[i]]];
        }
        return (NSArray*)ma; 
    }
    

    Example caller:

    static float floats[] = {1.1, 2.2, 3.3}; 
    NSUInteger count = sizeof(floats)/sizeof(float);
    NSArray* a = arrayFromFloats(count, floats); 
    
    0 讨论(0)
  • 2021-02-12 20:30

    going off on a slight tangent, if all you want to do is store an array of floats and don't really need the added functionality of an NSArray or NSNumber, then you should also consider just a standard C array:

    float fatArray[] = {6.6, 6.9, 4.7, 6.9};
    
    0 讨论(0)
  • 2021-02-12 20:40

    As mouviciel already wrote, this is the way to do it. When I write something like this I usually make the code shorter using a simple macro:

    #define FBOX(x) [NSNumber numberWithFloat:x]
    

    Then you can rewrite the code like this:

    NSArray *fatArray = [NSArray arrayWithObjects:
        FBOX(6.9), FBOX(4.7), FBOX(6.6), FBOX(6.9), nil];
    

    Macros are evil, but in this case the macro is so simple I’d use it. Plus the code hurts a bit less to read, especially if the macro definition is not far.

    If you wrote a lot code like this, you could create a category with a custom initializer with variable number of float arguments, but then there’s a problem ending the argument list. You could pass the total number of floats first:

    - (id) initWithFloats: (int) numFloats data: (float) float1, ...;
    

    But counting the arguments by hand is prone to error. Or you could use some sentinel value such as zero that would mark the end of the argument list, but this opens a whole new can of worms called floating-point comparison.


    Please note that nowadays you can simply write the following:

    NSArray *list = @[@6.9, @4.7, @6.6, @6.9];
    

    It’s not a syntax dream come true, but it’s officially supported by the compiler and it’s much better than the previous solutions. See the documentation for more goodness.

    0 讨论(0)
  • 2021-02-12 20:42

    As far as I know, this is a perfectly valid way of putting floats in an NSArray.

    0 讨论(0)
  • 2021-02-12 20:44

    Another lazy option is to just use an array of NSStrings and then getting the type of value you are looking for directly from the string.

    Like so:

    NSArray *arrayOfNumbers = [NSArray arrayWithObjects:@"3.12",@"3.2", @"12", @"15", nil];
    float number = [[arrayOfNumbers objectAtIndex:0] floatValue];
    
    0 讨论(0)
  • 2021-02-12 20:47

    I agree with mouviciel. Since you can't put simple types into an Array this is the way to do it. Looks a bit strange and is a lot of code to write but it is fine.

    0 讨论(0)
提交回复
热议问题