Note that there are some explanatory texts on larger screens.

plurals
  1. POAsynchronous initialization in Objective-C
    primarykey
    data
    text
    <p>I am currently making an Objective-C library for an API. To easily manage the data, I have created some classes acting as models.</p> <p>For example, I have an <code>Account</code> class which contains all the data concerning one specific account. I would like to make it easy to init this class and I thought about something like this :</p> <pre><code>@interface Account : NSObject @property (nonatomic, readonly) NSUInteger accountID; @property (nonatomic, readonly) NSString *username; // Other properties... + (instancetype)accountWithUsername:(NSString *)username success:(void (^)(Account *))success failure:(void (^)(NSError *))failure; - (instancetype)initWithUsername:(NSString *)username success:(void (^)(Account *))success failure:(void (^)(NSError *))failure; @end </code></pre> <p> </p> <pre><code>@implementation Account + (instancetype)accountWithUsername:(NSString *)username success:(void (^)(Account *))success failure:(void (^)(NSError *))failure { return [[JPImgurAccount alloc] initWithUsername:username success:success failure:failure]; } - (instancetype)initWithUsername:(NSString *)username success:(void (^)(Account *))success failure:(void (^)(NSError *))failure { self = [super init]; // Launch asynchronous requests, the callback will be called when it's finished return self; // Returning an empty object until the asynchronous request is finished } @end </code></pre> <p>However, returning an empty object through the <code>init</code> method bothers me a bit and I ask myself if it's a really good idea but I can't find why it could be risky.</p> <p>So I'm asking you: can I use this structure? If not, why? Should I go in classic way with a single <code>init</code> method coupled to a <code>loadWithUsername:(NSString *)username success:(void (^)(JPImgurAccount *))success failure:(void (^)(NSError *))failure</code> method?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload