* 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
*/
#import <Foundation/Foundation.h>
+#import "Word.h"
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)
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 */; };
/* Begin PBXFileReference section */
08FB7796FE84155DC02AAC07 /* Dictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Dictionary.m; sourceTree = "<group>"; };
08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
+ 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 = "<group>"; };
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 = "<group>"; };
children = (
32A70AAB03705E1F00C91783 /* Dictionary_Prefix.pch */,
08FB7796FE84155DC02AAC07 /* Dictionary.m */,
+ 1EB0367D0DE319BE00880828 /* Word.m */,
+ 1EB0367E0DE319BE00880828 /* Word.h */,
);
name = Source;
sourceTree = "<group>";
buildActionMask = 2147483647;
files = (
8DD76F9A0486AA7600D96B5E /* Dictionary.m in Sources */,
+ 1EB0367F0DE319BE00880828 /* Word.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
1DEB927908733DD40010E9CD /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
PREBINDING = NO;
ppc,
i386,
);
+ GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
PREBINDING = NO;
089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
- 1EAAC7960D16E9950058A23D /* dictionary.ka */ = {isa = PBXFileReference; lastKnownFileType = file; path = dictionary.ka; sourceTree = "<group>"; };
+ 1EAAC7960D16E9950058A23D /* dictionary.ka */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = dictionary.ka; sourceTree = "<group>"; };
1EAAC7990D16E9CE0058A23D /* AppController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppController.h; path = Source/AppController.h; sourceTree = "<group>"; };
1EAAC79A0D16E9CE0058A23D /* AppController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AppController.m; path = Source/AppController.m; sourceTree = "<group>"; };
1EAAC8400D16F4A40058A23D /* Word.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Word.h; path = Source/Word.h; sourceTree = "<group>"; };
@interface Word : NSObject
{
NSString *word;
+ NSString *alphagram;
}
- (id)initWithWord:(NSString *)word;
- (NSString *)word;
+- (NSString *)alphagram;
- (int)length;
@end
#import "Word.h"
+@interface Word (Private)
+- (void)createAlphagram;
+@end
+
@implementation Word
{
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
*/
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:%@>", word];
+ return [NSString stringWithFormat:@"<Word:%@/%@>", word, alphagram];
}
@end