简体   繁体   中英

Writing an SDK in Objective-C

Basically, I want to make api calls using an SDK I am writing.

I have the following classes:

  • Car
  • CarData (stores input values needed to create a car like model, make, etc)

Basically to create a car I do the following:

[Car carWithData: cardata onSuccess: successHandler onError: errorHandler] 

that basically is a factory method that creates instance of Car after making an API call request and populating the new Car class with the response and passes that instance to the successHandler.

So "Car" has the above static method to create that car, but also has non-static methods to edit, delete cars (which would make edit, delete API calls to the server)

So when the Car create static method passes a new car to the successHandler by doing the following:

successHandler([[Car alloc] initWithDictionary: dictionary)

The success handler can go ahead and use that new car to do the following:

[car update: cardata]
[car delete]

considering the new car object now has an ID for each car that it can pass to the update and delete API calls.

My questions:

  • Do I need a cardata object to store user inputs or can I store them in the car object that would also later store the response from all of the api calls?
  • How can I improve this model?

With regards to CarData, note that there might be different inputs for the different API calls. So create function might need to know model, make, etc, but find function might need to know the number of items to find, the limit, the start id, etc.

First, names are very important in building an SDK. Your names are a bit confusing. CarData sounds very much like it is related to an NSData . The typical name for "a class that holds information about another class" is a "descriptor." So, I would call this CarDescriptor . See UIFont and UIFontDescriptor for inspiration.

Next, your carWithData:onSuccess:onError: very much sounds like it should return a Car , but it appears to be void . I recommend the following:

+ (void)createCarWithDescriptor:(CarDescriptor *)descriptor 
                     completion:(void (^)(Car *car, NSError *error))completion;

It is preferred to have a single completion block, rather than two. Do not prefix completion handlers with on .

The CarDescriptor class should help you with your searching question as well, but it should not include things like limits. You should wrap that in a CarSearchRequest (or possibly just a CarSearch ). See NSFetchRequest for inspiration. Whether you pass a CarDescriptor or need a full predicate depends on how your service manages searches. (Of course, you could always have code that converts a CarDescriptor into a predicate.) Again, look at how UIFontDescriptor is used to search for fonts for inspiration.

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