Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created February 15, 2021 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/abd3b608b247c3307bd6d7d7484d9a8b to your computer and use it in GitHub Desktop.
Save robertmryan/abd3b608b247c3307bd6d7d7484d9a8b to your computer and use it in GitHub Desktop.
// InvoiceLine.h
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface InvoiceItem : NSObject
@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) NSString *itemDescription; // because `description` has special meaning in `NSObject`, we’ll call this `itemDescription`
@property (nonatomic, copy, nullable) NSString *cost;
@property (nonatomic, copy, nullable) NSString *tax;
@end
NS_ASSUME_NONNULL_END
@robertmryan
Copy link
Author

And you could use it like so:

InvoiceItem *item = [[InvoiceItem alloc] init];

...

item.title = "foo";

@robertmryan
Copy link
Author

And the .m file is pretty simple, too.

//  InvoiceItem.m

#import "InvoiceItem.h"

@implementation InvoiceItem

- (NSString *)description {
    return [NSString stringWithFormat:@"<InvoiceLine %p; title=%@; itemDescription=%@; cost=%@; tax=%@>", self, self.title, self.itemDescription, self.cost, self.tax];
}

@end

You don't need to implement description, but it makes it so you can easily log it, e.g.

InvoiceItem *item = [[InvoiceItem alloc] init];

...

item.title = "foo";
NSLog(@"%@", item);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment