When you run the Remodel script, it will use this information to generate an Objective-C object that has a description and coding. ![]() To use Remodel, define your data's schema in a simple format. Tedious tasks tend to be done inattentively, and that's when mistakes are introduced. Boilerplate isn't just annoying, it's dangerous. ImageUrl:(NSURL this point, you will have referenced each attribute seven times (twice in the header, twice in initializer implementation, once in encode, once in decode, and once in description). (instancetype)initWithUserId:(NSUInteger)userId To do this, you'll have to label the properties readonly and pass the values into an User : (nonatomic, readonly) NSUInteger (nonatomic, readonly) NSString (nonatomic, readonly) NSURL *imageUrl Often, you’ll also want your data to be immutable so that you can pass it around with the confidence that no other objects will change it (more on that later). Since decodeObjectForKey simply returns an object of type id, it's very possible to wind up with an object of the wrong type, which could cause a crash down the line. ![]() [aCoder encodeInteger:_userId encodeObject:_nickname encodeObject:_imageUrl careful treatment, these methods can easily run into problems, like trying to restore the wrong value for a given key. (void)encodeWithCoder:(NSCoder *)aCoder If you want to cache this data locally to avoid refetching it from the server, you'll have to write the ability to code/decode the object. Getting a message that is more useful for debugging requires writing a description method: For example, when you log this object, you'll get a message that doesn't show the object's contents, which can be useful when debugging: For User, this class might look something like the User : (nonatomic) NSUInteger (nonatomic) NSString (nonatomic) NSURL seems workable, but soon enough you'll find that there other things you'd like to do with the object. This addresses most of the concerns, in that you can now have strong typing around your data and feel more comfortable passing it around your application. One way to avoid the drawbacks of the dictionary approach is to write your own class for the data. Relying on the compiler is an important part of working in iOS, and the benefits of compile-time errors over runtime errors should not be disregarded. If someone decided to pass an NSNumber as the nickname value, the compiler would successfully build your application, yet a crash would be likely when your app is run. If you take this approach, you'll quickly run into type safety issues - the compiler won't help you be sure that, for example, a dictionary passed into your method would contain the entries you're expecting or that they're the right type. Once data like this winds up in the network layer, you're going to have to figure out how to pass it around to other parts of the system. ![]() Say you have an app that shows users, which receives a JSON response that looks like: In addition to introducing the fundamentals of Remodel, this post will cover the architectural benefits of building a system with immutable value models. Today, we're excited to announce that we are open-sourcing Remodel. To solve these problems, we built Remodel, an Objective-C code generation tool specialized for quickly creating and editing model objects.Īs our codebase scaled, we found that giving these objects certain properties enforced good patterns (such as the separation of data and behavior), enabled a simpler programming model, allowed us to explore lock-free multi-threading strategies, and simplified unit test writing. We also found that maintaining custom model objects was time-consuming, tedious, and error-prone. Passing dictionaries of data around proved dangerous because dictionary fields are not type-safe. We've seen these patterns in our use of Objective-C at Facebook, and we've found some drawbacks in these approaches. Many iOS developers use dictionaries for this purpose, or use handwritten, simple objects instead. ![]() One of the aspects of building an application is deciding how data will flow through it.
0 Comments
Leave a Reply. |