Skip to content

Commit

Permalink
Added a Serializable mixin example.
Browse files Browse the repository at this point in the history
  • Loading branch information
vl4dimir committed Apr 14, 2011
1 parent 01c2025 commit f58fc1a
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Examples/Serializable.h
@@ -0,0 +1,26 @@
//
// Serializable.h
// ObjectiveMixin
//
// Created by Vladimir Mitrovic on 14/04/2011.
// Copyright 2011 Vladimir Mitrovic. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface Serializable : NSObject {}
- (void) serializeToFile:(NSString*)path;
- (void) deserializeFromFile:(NSString*)path;
@end


@protocol SerializableMixin <NSObject>
@required
- (NSString*) serializedRepresentation;
- (void) deserialize:(NSString*)serialized;

@optional
- (void) serializeToFile:(NSString*)path;
- (void) deserializeFromFile:(NSString*)path;
@end
24 changes: 24 additions & 0 deletions Examples/Serializable.m
@@ -0,0 +1,24 @@
//
// Serializable.m
// ObjectiveMixin
//
// Created by Vladimir Mitrovic on 14/04/2011.
// Copyright 2011 Vladimir Mitrovic. All rights reserved.
//

#import "Serializable.h"


@implementation Serializable

- (void) serializeToFile:(NSString*)path {
NSString* serialized = [(id)self serializedRepresentation];
[serialized writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

- (void) deserializeFromFile:(NSString*)path {
NSString* serialized = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[(id)self deserialize:serialized];
}

@end
12 changes: 12 additions & 0 deletions ObjectiveMixin.xcodeproj/project.pbxproj
Expand Up @@ -24,6 +24,8 @@
E77355E1135721C90062EB8A /* CandyFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = E77355E0135721C90062EB8A /* CandyFactory.m */; };
E77355E4135722830062EB8A /* Singleton.m in Sources */ = {isa = PBXBuildFile; fileRef = E77355E3135722830062EB8A /* Singleton.m */; };
E77355ED135723850062EB8A /* Mixin.m in Sources */ = {isa = PBXBuildFile; fileRef = E7DE3B5513165FB500C96724 /* Mixin.m */; };
E77355F1135724660062EB8A /* Serializable.m in Sources */ = {isa = PBXBuildFile; fileRef = E77355F0135724660062EB8A /* Serializable.m */; };
E77355F4135727950062EB8A /* MountainBike.m in Sources */ = {isa = PBXBuildFile; fileRef = E77355F3135727940062EB8A /* MountainBike.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -68,6 +70,10 @@
E77355E0135721C90062EB8A /* CandyFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CandyFactory.m; sourceTree = "<group>"; };
E77355E2135722830062EB8A /* Singleton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Singleton.h; sourceTree = "<group>"; };
E77355E3135722830062EB8A /* Singleton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Singleton.m; sourceTree = "<group>"; };
E77355EF135724660062EB8A /* Serializable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Serializable.h; sourceTree = "<group>"; };
E77355F0135724660062EB8A /* Serializable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Serializable.m; sourceTree = "<group>"; };
E77355F2135727940062EB8A /* MountainBike.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MountainBike.h; sourceTree = "<group>"; };
E77355F3135727940062EB8A /* MountainBike.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MountainBike.m; sourceTree = "<group>"; };
E7DE3B4713165EA600C96724 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
E7DE3B5413165FB500C96724 /* Mixin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Mixin.h; sourceTree = "<group>"; };
E7DE3B5513165FB500C96724 /* Mixin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Mixin.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -182,6 +188,8 @@
children = (
E77355E2135722830062EB8A /* Singleton.h */,
E77355E3135722830062EB8A /* Singleton.m */,
E77355EF135724660062EB8A /* Serializable.h */,
E77355F0135724660062EB8A /* Serializable.m */,
);
name = "Example Mixin Classes";
path = Examples;
Expand All @@ -194,6 +202,8 @@
E77355D7135721700062EB8A /* main.m */,
E77355DF135721C90062EB8A /* CandyFactory.h */,
E77355E0135721C90062EB8A /* CandyFactory.m */,
E77355F2135727940062EB8A /* MountainBike.h */,
E77355F3135727940062EB8A /* MountainBike.m */,
);
path = ObjectiveMixinExamples;
sourceTree = "<group>";
Expand Down Expand Up @@ -431,6 +441,8 @@
E77355E1135721C90062EB8A /* CandyFactory.m in Sources */,
E77355E4135722830062EB8A /* Singleton.m in Sources */,
E77355ED135723850062EB8A /* Mixin.m in Sources */,
E77355F1135724660062EB8A /* Serializable.m in Sources */,
E77355F4135727950062EB8A /* MountainBike.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
23 changes: 23 additions & 0 deletions ObjectiveMixinExamples/MountainBike.h
@@ -0,0 +1,23 @@
//
// MountainBike.h
// ObjectiveMixin
//
// Created by Vladimir Mitrovic on 14/04/2011.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Serializable.h"

@interface MountainBike : NSObject <SerializableMixin> {
NSString* make;
NSString* model;
}

@property (nonatomic, retain) NSString* make;
@property (nonatomic, retain) NSString* model;

- (NSString*) serializedRepresentation;
- (void) deserialize:(NSString*)serialized;

@end
30 changes: 30 additions & 0 deletions ObjectiveMixinExamples/MountainBike.m
@@ -0,0 +1,30 @@
//
// MountainBike.m
// ObjectiveMixin
//
// Created by Vladimir Mitrovic on 14/04/2011.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MountainBike.h"
#import "Mixin.h"

@implementation MountainBike
@synthesize make;
@synthesize model;

+ (void) initialize {
[Mixin from:[Serializable class] into:self];
}

- (NSString*) serializedRepresentation {
return [NSString stringWithFormat:@"%@,%@", self.make, self.model];
}

- (void) deserialize:(NSString*)serialized {
NSArray* components = [serialized componentsSeparatedByString:@","];
self.make = [components objectAtIndex:0];
self.model = [components objectAtIndex:1];
}

@end
15 changes: 15 additions & 0 deletions ObjectiveMixinExamples/main.m
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>
#import "CandyFactory.h"
#import "MountainBike.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Expand All @@ -23,6 +24,20 @@ int main (int argc, const char * argv[]) {
[CandyFactory destroyInstance];
}

// Test the serializable mixin. We create a MountainBike instance, serialize it
// to disk, and then populate another instance by deserializing from that file.
MountainBike* bike = [[[MountainBike alloc] init] autorelease];
bike.make = @"Scott";
bike.model = @"Scale 30";

NSString* path = @"bike.txt";
[bike serializeToFile:path];

MountainBike* anotherBike = [[[MountainBike alloc] init] autorelease];
[anotherBike deserializeFromFile:path];
NSLog(@"Original bike: %@ %@", bike.make, bike.model);
NSLog(@"Deserialized bike: %@ %@", anotherBike.make, anotherBike.model);

[pool drain];
return 0;
}
Expand Down

0 comments on commit f58fc1a

Please sign in to comment.