简体   繁体   中英

Objective-C: How to get properties of an object inside NSMutableArray?

I have this NSMutableArray of UIImage named camImages . Then I want to get the scale property of an image, but I can't seem to access that using

[[camImages objectAtIndex:i] scale] //doesn't return the desired scale property
[camImages.objectAtIndex:i].scale //doesn't work (Error: Property 'scale' not found on object of type 'id')

whereas it is possible to get the property if I have a single UIImage

UIImage *img;
img.scale //desired property

I am newbie to iOS & Objective-C, how can I get the desired property? Thanks in advance!

EDIT:

[[camImages objectAtIndex:i] scale] will return

NSDecimalNumberBehaviors

Scale

Returns the number of digits allowed after the decimal separator. (required)

  • (short)scale Return Value The number of digits allowed after the decimal separator.

whereas the desired scale is of CGFloat type:

UIImage

scale

The scale factor of the image. (read-only)

@property(nonatomic, readonly) CGFloat scale Discussion If you load an image from a file whose name includes the @2x modifier, the scale is set to 2.0. You can also specify an explicit scale factor when initializing an image from a Core Graphics image. All other images are assumed to have a scale factor of 1.0.

If you multiply the logical size of the image (stored in the size property) by the value in this property, you get the dimensions of the image in pixels.

Make your code easier to read and debug:

UIImage *image = camImages[i];
CGFloat scale = image.scale;

if you have two scale method with different signature, compiler may not able to choose the correct signature to use, so you have to tell compiler the type of the object so it can find the correct one

if you really want one line solution

[((UIImage *)[camImages objectAtIndex:i]) scale];
((UIImage *)[camImages objectAtIndex:i]).scale;

but use @rmaddy answer for readability

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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