Initialize NSArray with floats?

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

    Example caller:

    static float floats[] = {1.1, 2.2, 3.3}; 
    NSUInteger count = sizeof(floats)/sizeof(float);
    NSArray* a = arrayFromFloats(count, floats); 
    

提交回复
热议问题