【问题标题】:iPhone - encode Array of Data from CoreData Entity and store in Base64 StringiPhone - 编码来自 CoreData 实体的数据数组并存储在 Base64 字符串中
【发布时间】:2010-02-20 15:53:53
【问题描述】:

我有一个包含三个实体(Purchase、Items、Image)的数据模型,其中购买是主要的,另外两个作为相关属性。

我需要能够将数据发送到 Web 服务器,并且我必须使用 base64 进行编码,我有几个类负责我从网上下载的编码和解码,它们是NSData 类,但是当我使用它们时,我收到一个空字符串。

     // For Error Information
 NSError *error;

 // Create a File Manager
 NSFileManager *fileManager = [NSFileManager defaultManager];

 // Point to the Documents Directory
 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

 // File we want to create in the Documents Directory
 NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"dataArray.txt"];

 NSString *joined = [self.dataList componentsJoinedByString:@","];

 NSData *myData = [NSData dataWithBase64EncodedString:joined];
 NSLog(@"%@", myData); // THIS IS NULL
 [myData writeToFile:filePath atomically:YES];

所以数组吐出这个...

   <Purchase: 0x502b7a0> (entity: Purchase; id: 0x502a620 <x-coredata://92618241-3890-45FD-B1D7-7F17D52CAF38/Purchase/p22> ; data: {
    amount = 123.56;
    image = 0x502c7b0 <x-coredata://92618241-3890-45FD-B1D7-7F17D52CAF38/Image/p11>;
    items = "<relationship fault: 0x5024900 'items'>";
    latitude = 37.331689;
    longitude = -122.030731;
    name = "Macy's";
    thumbnailImage = "(...not nil..)";
    timestamp = 2010-02-18 18:03:03 -0500;
    type = nil;
}),<Purchase: 0x502bdf0> (entity: Purchase; id: 0x502a630 <x-coredata://92618241-3890-45FD-B1D7-7F17D52CAF38/Purchase/p20> ; data: {
    amount = 12.34;
    image = 0x502c8e0 <x-coredata://92618241-3890-45FD-B1D7-7F17D52CAF38/Image/p9>;
    items = "<relationship fault: 0x502ef40 'items'>";
    latitude = 37.331689;
    longitude = -122.030731;
    name = Rippers;
    thumbnailImage = "(...not nil..)";
    timestamp = 2010-02-18 12:18:34 -0500;
    type = Travel;
}),<Purchase: 0x502c0e0> (entity: Purchase; id: 0x502a640 <x-coredata://92618241-3890-45FD-B1D7-7F17D52CAF38/Purchase/p21> ; data: {
    amount = 5.56;
    image = 0x502ca40 <x-coredata://92618241-3890-45FD-B1D7-7F17D52CAF38/Image/p10>;
    items = "<relationship fault: 0x502f6a0 'items'>";
    latitude = 37.331689;
    longitude = -122.030731;
    name = "Roy Rogers";
    thumbnailImage = "(...not nil..)";
    timestamp = 2010-02-18 17:45:03 -0500;
    type = Clothing;
}),<Purchase: 0x502c320> (entity: Purchase; id: 0x502a650 <x-coredata://92618241-3890-45FD-B1D7-7F17D52CAF38/Purchase/p16> ; data: {
    amount = 5.52;
    image = nil;
    items = "<relationship fault: 0x502f9e0 'items'>";
    latitude = 37.331689;
    longitude = -122.030731;
    name = heariam;
    thumbnailImage = nil;
    timestamp = 2010-02-18 09:41:35 -0500;
    type = nil;
})

如何将其编码为 base64 字符串,以便通过电子邮件将其发送到网络服务器

非常感谢任何帮助,甚至是正确方向的一点,我对编程还是很陌生。

【问题讨论】:

    标签: iphone base64 nsdata encode decode


    【解决方案1】:

    链接器是否向您发出警告“NSData 可能无法响应 dataWithBase64EncodedString”?

    如果是这样,问题是您的类别没有正确导入,并且就该代码段而言,该方法不存在。确保将类别导入到标题或上面代码所属的任何类的实现中。

    编辑01

    我刚刚注意到您加载了一个 NSManagedObjects 数组。我不认为 N​​SData 可以转换无故障的托管对象。您必须在包括关系在内的所有属性上触发故障。

    尝试设置用于获取对象的 fetch 以返回出错的对象。

    [dataListFetch setReturnsObjectsAsFaults:NO]; // or yes, I always get the two reversed.
    

    当您打印出错误的 NSManagedObject 时,关系看起来不像:

    items = "<relationship fault: 0x5024900 'items'>";
    

    相反,它将对象转储到关系的另一端。如果您的图表非常复杂,您可以获得非常广泛的转储。

    核心数据图可能非常复杂且难以序列化/存档。事实上,Core Data 的存在是为了处理无法用数组和字典轻松表示的图形。

    为了使图表足够简单以供上传,您可能必须创建数组或字典对象并使用托管对象的值填充它们。然后将其转换为数据。

    【讨论】:

    • 不,这不是问题,分类是导入的,字符串只是返回(null)
    • 查看我的编辑以获取其他建议。如果这能解决您的问题,请记得点击复选标记。
    猜你喜欢
    • 2016-04-19
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多