在文本文件中写一个NSMutableArray

时间:2014-07-09 16:07:27

标签: objective-c macos cocoa

我试图在txt文件中写一个表,但我的代码似乎无法正常工作。

    Action *action1 = [[Action alloc] init];
    Action *action2 = [[Action alloc] init];
    ActionEtrangere *action3 = [[ActionEtrangere alloc] init];

    [action1 setPrixAchatAction:2.30];
    [action1 setPrixActuelAction:4.50];
    [action1 setNbActions:40];

    [action2 setPrixAchatAction:12.19];
    [action2 setPrixActuelAction:10.59];
    [action2 setNbActions:90];

    [action3 setPrixAchatAction:45.10];
    [action3 setPrixActuelAction:49.51];
    [action3 setNbActions:210];
    [action3 setTauxConversion:0.94];

    NSMutableArray *porteMonnaie = [[NSMutableArray alloc] initWithObjects:action1, action2, action3, nil];

    for (Action *p in porteMonnaie) {
        NSLog(@"Le cout en euros est de %.2f et la valeur en euro de %.2f", [p coutEnEuros], [p valeurEnEuros]);
    }

    //save des valeurs
    BOOL save = [porteMonnaie writeToFile:@"/tmp/save.txt" atomically:YES];

    if (!save) {
        NSLog(@"Error Save");
    } else {
        NSLog(@"Save ok");
    }

}
return 0;

}

如果你有一些想法!也许我不能在txt中写一个mutable?

感谢

3 个答案:

答案 0 :(得分:1)

您可能希望将文件保存到应用程序的沙箱中,我想:

NSString *_filename = @"save.txt"
NSURL *_resourceURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:_fileName];

我还假设数组中的所有项都符合NSCoding协议,因此现在应该可以使用:

NSMutableArray *porteMonnaie = [[NSMutableArray alloc] initWithObjects:action1, action2, action3, nil];
BOOL _isSaved = [porteMonnaie writeToFile:[_resourceURL path] atomically:TRUE];

答案 1 :(得分:0)

您的数组未写入文件的主要原因: -

根据documentationwriteToFile:automatically:仅在数组内容类型为(NSString, NSData, NSArray, or NSDictionary objects)时才有效。如果您的数组中有任何其他类型的对象,则它将无法正确写入文件。并且bool标志将始终返回

答案 2 :(得分:0)

writeToFile:automatically:无法将自定义对象写入磁盘。

您可以通过实施NSCoding协议并使用NSKeyedArchiver写入和NSKeyedUnarchiver来从磁盘读取来实现。

协议有两个必须实现的方法。

-(id)initWithCoder:(NSCoder *)decoder-(void)encodeWithCoder:(NSCoder *)encoder

对于行动,它可能是

@implementation Action

-(id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (!self) {
        return nil;
    }

    self.prixAchatAction = [[decoder decodeObjectForKey:@"prixAchatAction"] doubleValue];
    self.prixActuelAction = [[decoder decodeObjectForKey:@"prixActuelAction"] doubleValue];
    self.nbActions = [decoder decodeIntegerForKey:@"nbActions"];

    return self;
}

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:@(self.prixAchatAction) forKey:@"prixAchatAction"];
    [encoder encodeObject:@(self.prixActuelAction) forKey:@"prixActuelAction"];
    [encoder encodeInteger:self.nbActions forKey:@"nbActions"];

}

@end

对于ActionEtrangere

@interface ActionEtrangere : Action
@property (nonatomic, assign) CGFloat tauxConversion;
@end

@implementation ActionEtrangere
-(id)initWithCoder:(NSCoder *)decoder
{
    self = [super initWithCoder:decoder];
    if (!self) {
        return nil;
    }
    self.tauxConversion = [[decoder decodeObjectForKey:@"tauxConversion"] doubleValue];

    return self;
}

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [super encodeWithCoder:encoder];
    [encoder encodeObject:@(self.tauxConversion) forKey:@"tauxConversion"];
}

现在你可以写下你的portemonnaie:

BOOL success = [NSKeyedArchiver archiveRootObject:porteMonnaie
                                           toFile:[NSHomeDirectory() stringByAppendingPathComponent:@"portemonnaie"]];
if(!success) {
    NSLog(@"something went wrong");
}

读起来像:

NSArray *newporteMonnaie = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSHomeDirectory() stringByAppendingPathComponent:@"portemonnaie"]];

完整的命令行程序:


#import <Foundation/Foundation.h>


@interface Action : NSObject <NSCoding>
@property (nonatomic, assign) CGFloat prixAchatAction;
@property (nonatomic, assign) CGFloat prixActuelAction;
@property (nonatomic, assign) NSInteger nbActions;

@end

@implementation Action

-(id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (!self) {
        return nil;
    }

    self.prixAchatAction = [[decoder decodeObjectForKey:@"prixAchatAction"] doubleValue];
    self.prixActuelAction = [[decoder decodeObjectForKey:@"prixActuelAction"] doubleValue];
    self.nbActions = [decoder decodeIntegerForKey:@"nbActions"];

    return self;
}

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:@(self.prixAchatAction) forKey:@"prixAchatAction"];
    [encoder encodeObject:@(self.prixActuelAction) forKey:@"prixActuelAction"];
    [encoder encodeInteger:self.nbActions forKey:@"nbActions"];

}

@end


@interface ActionEtrangere : Action
@property (nonatomic, assign) CGFloat tauxConversion;
@end

@implementation ActionEtrangere
-(id)initWithCoder:(NSCoder *)decoder
{
    self = [super initWithCoder:decoder];
    if (!self) {
        return nil;
    }
    self.tauxConversion = [[decoder decodeObjectForKey:@"tauxConversion"] doubleValue];

    return self;
}

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [super encodeWithCoder:encoder];
    [encoder encodeObject:@(self.tauxConversion) forKey:@"tauxConversion"];
}


@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        Action *action1 = [[Action alloc] init];
        Action *action2 = [[Action alloc] init];
        ActionEtrangere *action3 = [[ActionEtrangere alloc] init];

        [action1 setPrixAchatAction:2.30];
        [action1 setPrixActuelAction:4.50];
        [action1 setNbActions:40];

        [action2 setPrixAchatAction:12.19];
        [action2 setPrixActuelAction:10.59];
        [action2 setNbActions:90];

        [action3 setPrixAchatAction:45.10];
        [action3 setPrixActuelAction:49.51];
        [action3 setNbActions:210];
        [action3 setTauxConversion:0.94];

        NSArray *porteMonnaie = @[action1, action2,action3];

        BOOL success = [NSKeyedArchiver archiveRootObject:porteMonnaie
                                                   toFile:[NSHomeDirectory() stringByAppendingPathComponent:@"portemonnaie2.plist"]];
        if(!success) {
            NSLog(@"something went wrong");
        } else {
            NSArray *newporteMonnaie = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSHomeDirectory() stringByAppendingPathComponent:@"portemonnaie2.plist"]];

        }
    }
    return 0;
}