Is this a good model class (Cocoa-like, approved by Apple)?

I used Objective-C for a while, but I did not adhere to Apple principles very well. I recently read Cocoa Design Patterns and the Model Object Implementation Guide , and I'm trying to do some very simple things, but I am doing them very well.

Did I miss any basic concepts? Please do not mention self = [super init]; which has been covered so many times by SO. Feel free to criticize mine #pragma markthough!

#import "IRTileset.h"
#import "IRTileTemplate.h"

@interface IRTileset () //No longer lists protocols because of Felixyz

@property (retain) NSMutableArray* tileTemplates; //Added because of TechZen

@end

#pragma mark -
@implementation IRTileset

#pragma mark -
#pragma mark Initialization

- (IRTileset*)init
{
    if (![super init])
    {
        return nil;
    }

    tileTemplates = [NSMutableArray new];

    return self;
}

- (void)dealloc
{
    [tileTemplates release];
    [uniqueID release]; //Added because of Felixyz (and because OOPS. Gosh.)
    [super dealloc]; //Moved from beginning to end because of Abizern
}

#pragma mark -
#pragma mark Copying/Archiving

- (IRTileset*)copyWithZone:(NSZone*)zone
{
    IRTileset* copy = [IRTileset new];
    [copy setTileTemplates:tileTemplates]; //No longer insertTileTemplates: because of Peter Hosey
    [copy setUniqueID:uniqueID];

    return copy; //No longer [copy autorelease] because of Jared P
}

- (void)encodeWithCoder:(NSCoder*)encoder
{
    [encoder encodeObject:uniqueID forKey:@"uniqueID"];
    [encoder encodeObject:tileTemplates forKey:@"tileTemplates"];
}

- (IRTileset*)initWithCoder:(NSCoder*)decoder
{
    [self init];

    [self setUniqueID:[decoder decodeObjectForKey:@"uniqueID"]];
    [self setTileTemplates:[decoder decodeObjectForKey:@"tileTemplates"]]; //No longer insertTileTemplates: because of Peter Hosey

    return self;
}

#pragma mark -
#pragma mark Public Accessors

@synthesize uniqueID;
@synthesize tileTemplates;

- (NSUInteger)countOfTileTemplates
{
    return [tileTemplates count];
}

- (void)insertTileTemplates:(NSArray*)someTileTemplates atIndexes:(NSIndexSet*)indexes
{
    [tileTemplates insertObjects:someTileTemplates atIndexes:indexes];
}

- (void)removeTileTemplatesAtIndexes:(NSIndexSet*)indexes
{
    [tileTemplates removeObjectsAtIndexes:indexes];
}

//These are for later.
#pragma mark -
#pragma mark Private Accessors

#pragma mark -
#pragma mark Other

@end

(Edit: I made the changes proposed so far and commented on which answers discuss them, in case anyone needs to know why.)

+3
4

, self = [super init]...

, ?

initWithCoder:: , [self init], , .

- (void)dealloc
{
    [super dealloc];
    [tileTemplates release];
}

, [super dealloc] . .

- (IRTileTemplate*)copyWithZone:(NSZone*)zone

id, , NSCopying.

{
    IRTileset* copy = [IRTileset new];
    [copy insertTileTemplates:tileTemplates atIndexes:[NSIndexSet indexSetWithIndex:0]];
    [copy setUniqueID:uniqueID];

. : location = 0, length = count tileTemplates. , :

copy.tileTemplates = self.tileTemplates;

:

copy->tileTemplates = [tileTemplates copy];

( , copy copy .)

    return [copy autorelease];
}

copyWithZone: . , copy copyWithZone: , , , copyWithZone:.

@synthesize tileTemplates;
[et al]

, :

- (void) insertObjectInTileTemplates:(IRTileTemplate *)template atIndex:(NSUInteger)idx;
- (void) removeObjectFromTileTemplatesAtIndex:(NSUInteger)idx;

, .

+5

, , . , , .

, , - :

myIRTileset.tileTemplates=someArray;

, .

readonly. , , . , tileTemplates - - insertTileTemplates:atIndexes: removeTileTemplatesAtIndexes:.a >

Edit01:

, , . :

@interface PrivateTest : NSObject {
@private 
    //iVar is invisible outside the class, even its subclasses
    NSString *privateString; 
@public
    //iVar is visible and settable to every object. 
    NSString *publicString; 
}
@property(nonatomic, retain)  NSString *publicString; //property accessors are visible, settable and getable. 
//These methods control logical operations on the private iVar.
- (void) setPrivateToPublic;  
- (NSString *) returnPrivateString;
@end

, :

#import "PrivateTest.h"

//private class extension category defines 
// the propert setters and getters 
// internal to the class
@interface PrivateTest ()
@property(nonatomic, retain)  NSString *privateString;
@end

@implementation PrivateTest
//normal synthesize directives
@synthesize privateString; 
@synthesize publicString;

// Methods that control access to private
- (void) setPrivateToPublic{
    //Here we do a contrived validation test 
    if (self.privateString != nil) {
        self.privateString=self.publicString;
    }
}

- (NSString *) returnPrivateString{
    return self.privateString;
}

@end

:

PrivateTest *pt=[[PrivateTest alloc] init];
    // If you try to set private directly as in the next line
    // the complier throws and error
//pt.privateString=@"Bob"; ==> "object cannot be set - either readonly property or no setter found"
pt.publicString=@"Steve";
[pt setPrivateToPublic];
NSLog(@"private=%@",[pt returnPrivateString]); //==> "Steve"

. publicString, private.

, , , - .

+2

//, , IRTileset.h?

, . , , , , .

: _tileTemplates. ( , , , , .)

new . , .

[NSMutableArray new];                     //  :(
[NSMutableArray arrayWithCapacity:20];    //  :)

[super dealloc], ! .

- (void)dealloc
{
    [tileTemplates release];
    [super dealloc];          // Do this last
}

, uniqueID , dealloc?

@synthesize ( "@implementation" ).

, , countOfTileTemplates . , "count" , , ?

+2

nitpicks: - init ( , ), , init , , , . self = [super init], , , , , . -, copyWithZone tileTemplates, , , , ( ). , , , . 1, . , , - , , , -

- (IRTileTemplate*)copyWithZone:(NSZone*)zone {
    IRTileset* copy = [[IRTileset allocWithZone:zone] init];
    [copy insertTileTemplates:[tileTemplates copyWithZone:zone]
                    atIndexes:[NSIndexSet indexSetWithIndex:0]];
    [copy setUniqueID:uniqueID];
    return copy;
}

; ( ), , , , -, .

0

Source: https://habr.com/ru/post/1734715/


All Articles