Initialize NSArray with floats?

后端 未结 7 1805
情歌与酒
情歌与酒 2021-02-12 20:05

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:35

    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); 
    

提交回复
热议问题