From 7193fd0a7190e56c34f426c5a8010e199ff7f510 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 21 May 2008 16:54:46 -0400 Subject: [PATCH] Adding an alphagram ivar to Word which is calculated and then stored in the keyed archive * Dictionary/Dictionary.m: Create Word objects and archive them rather than NSStrings * Dictionary/Dictionary.xcodeproj: Switch to C99 mode and include Word.m/h * Scrabbalize.xcodeproj: Mysterious project updates? * Source/Word.h: Adding alphagram ivar and getter * Source/Word.m: (-[dealloc]): New method (-[initWithCoder:]): New method (-[encodeWithCoder:]): New method (-[alphagram]): New method (-[createAlphagram]): New method (-[description]): Include the alphagram in the description --- Dictionary/Dictionary.m | 12 +++- .../Dictionary.xcodeproj/project.pbxproj | 8 +++ Scrabbalize.xcodeproj/project.pbxproj | 2 +- Source/Word.h | 2 + Source/Word.m | 67 ++++++++++++++++++- 5 files changed, 87 insertions(+), 4 deletions(-) diff --git a/Dictionary/Dictionary.m b/Dictionary/Dictionary.m index 68ce216..4cd553b 100644 --- a/Dictionary/Dictionary.m +++ b/Dictionary/Dictionary.m @@ -15,6 +15,7 @@ */ #import +#import "Word.h" int main (int argc, const char * argv[]) { @@ -31,7 +32,16 @@ int main (int argc, const char * argv[]) file = [file stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"]; file = [file stringByReplacingOccurrencesOfString:@"\r" withString:@"\n"]; - NSArray *words = [file componentsSeparatedByString:@"\n"]; + NSArray *strings = [file componentsSeparatedByString:@"\n"]; + NSMutableArray *words = [[NSMutableArray alloc] init]; + for (int i = 0; i < [strings count]; i++) + { + NSString *str = [strings objectAtIndex:i]; + Word *word = [[Word alloc] initWithWord:str]; + [words addObject:word]; + NSLog(@"%@", word); + [word release]; + } BOOL result = [NSKeyedArchiver archiveRootObject:words toFile:@"dictionary.ka"]; if (result) diff --git a/Dictionary/Dictionary.xcodeproj/project.pbxproj b/Dictionary/Dictionary.xcodeproj/project.pbxproj index a74f498..7e0bc84 100644 --- a/Dictionary/Dictionary.xcodeproj/project.pbxproj +++ b/Dictionary/Dictionary.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 1EB0367F0DE319BE00880828 /* Word.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB0367D0DE319BE00880828 /* Word.m */; }; 8DD76F9A0486AA7600D96B5E /* Dictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* Dictionary.m */; settings = {ATTRIBUTES = (); }; }; 8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; }; 8DD76F9F0486AA7600D96B5E /* Dictionary.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859EA3029092ED04C91782 /* Dictionary.1 */; }; @@ -28,6 +29,8 @@ /* Begin PBXFileReference section */ 08FB7796FE84155DC02AAC07 /* Dictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Dictionary.m; sourceTree = ""; }; 08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = ""; }; + 1EB0367D0DE319BE00880828 /* Word.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Word.m; path = ../Source/Word.m; sourceTree = SOURCE_ROOT; }; + 1EB0367E0DE319BE00880828 /* Word.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Word.h; path = ../Source/Word.h; sourceTree = SOURCE_ROOT; }; 32A70AAB03705E1F00C91783 /* Dictionary_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dictionary_Prefix.pch; sourceTree = ""; }; 8DD76FA10486AA7600D96B5E /* Dictionary */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Dictionary; sourceTree = BUILT_PRODUCTS_DIR; }; C6859EA3029092ED04C91782 /* Dictionary.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = Dictionary.1; sourceTree = ""; }; @@ -61,6 +64,8 @@ children = ( 32A70AAB03705E1F00C91783 /* Dictionary_Prefix.pch */, 08FB7796FE84155DC02AAC07 /* Dictionary.m */, + 1EB0367D0DE319BE00880828 /* Word.m */, + 1EB0367E0DE319BE00880828 /* Word.h */, ); name = Source; sourceTree = ""; @@ -133,6 +138,7 @@ buildActionMask = 2147483647; files = ( 8DD76F9A0486AA7600D96B5E /* Dictionary.m in Sources */, + 1EB0367F0DE319BE00880828 /* Word.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -170,6 +176,7 @@ 1DEB927908733DD40010E9CD /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + GCC_C_LANGUAGE_STANDARD = c99; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; PREBINDING = NO; @@ -184,6 +191,7 @@ ppc, i386, ); + GCC_C_LANGUAGE_STANDARD = c99; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; PREBINDING = NO; diff --git a/Scrabbalize.xcodeproj/project.pbxproj b/Scrabbalize.xcodeproj/project.pbxproj index ba3a5f4..d5f49da 100644 --- a/Scrabbalize.xcodeproj/project.pbxproj +++ b/Scrabbalize.xcodeproj/project.pbxproj @@ -22,7 +22,7 @@ 089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = ""; }; 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = ""; }; 13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = ""; }; - 1EAAC7960D16E9950058A23D /* dictionary.ka */ = {isa = PBXFileReference; lastKnownFileType = file; path = dictionary.ka; sourceTree = ""; }; + 1EAAC7960D16E9950058A23D /* dictionary.ka */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = dictionary.ka; sourceTree = ""; }; 1EAAC7990D16E9CE0058A23D /* AppController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppController.h; path = Source/AppController.h; sourceTree = ""; }; 1EAAC79A0D16E9CE0058A23D /* AppController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AppController.m; path = Source/AppController.m; sourceTree = ""; }; 1EAAC8400D16F4A40058A23D /* Word.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Word.h; path = Source/Word.h; sourceTree = ""; }; diff --git a/Source/Word.h b/Source/Word.h index d377931..fa286e7 100644 --- a/Source/Word.h +++ b/Source/Word.h @@ -20,11 +20,13 @@ @interface Word : NSObject { NSString *word; + NSString *alphagram; } - (id)initWithWord:(NSString *)word; - (NSString *)word; +- (NSString *)alphagram; - (int)length; @end diff --git a/Source/Word.m b/Source/Word.m index 49fa0a0..0474923 100644 --- a/Source/Word.m +++ b/Source/Word.m @@ -16,6 +16,10 @@ #import "Word.h" +@interface Word (Private) +- (void)createAlphagram; +@end + @implementation Word @@ -26,11 +30,41 @@ { if (self = [super init]) { - word = aWord; + word = [aWord retain]; } return self; } +/** + * Destructor + */ +- (void)dealloc +{ + [word release]; + [alphagram release]; + [super dealloc]; +} + +/** + * Initialize an object from a keyed archive + */ +- (id)initWithCoder:(NSCoder *)coder +{ + self = [super init]; + word = [[coder decodeObjectForKey:@"KAWord"] retain]; + alphagram = [[coder decodeObjectForKey:@"KAAlphagram"] retain]; + return self; +} + +/** + * Encodes an object for serialization in a keyed archiver + */ +- (void)encodeWithCoder:(NSCoder *)coder +{ + [coder encodeObject:word forKey:@"KAWord"]; + [coder encodeObject:alphagram forKey:@"KAAlphagram"]; +} + /** * Returns the word */ @@ -47,12 +81,41 @@ return [word length]; } +/** + * Returns the alphagram + */ +- (NSString *)alphagram +{ + return alphagram; +} + +/** + * Generates the alphagram for the word + */ +- (void)createAlphagram +{ + char *str = (char *)[word UTF8String]; + + int j; + for (int i = 1; i < [word length]; i++) + { + char temp = str[i]; + for (j = i; j > 0 && temp < str[j - 1]; j--) + { + str[j] = str[j - 1]; + } + str[j] = temp; + } + + alphagram = [[NSString stringWithUTF8String:str] retain]; +} + /** * Description method */ - (NSString *)description { - return [NSString stringWithFormat:@"", word]; + return [NSString stringWithFormat:@"", word, alphagram]; } @end -- 2.43.5