小编n.e*_*ind的帖子

NSKeyedUnarchiver - 如何防止崩溃

我想这很明显,但我有一个关于加载数据的问题.如果有一个名为library.dat的文件,它存储有关应用程序中对象的所有类型的信息.它设置得很好(就initWithCoder和encodeWithCoder方法等而言),但我只是想知道如果library.dat被破坏会发生什么.我自己损坏了一点,然后应用程序崩溃了.有没有办法防止崩溃?我可以在加载前测试文件吗?这是可能非常致命的一点:

    -(void)loadLibraryDat {

    NSLog(@"loadLibraryDat...");
    NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"library.dat"];

    // if the app crashes here, there is no way for the user to get the app running- except by deleting and re-installing it...
    self.libraryDat = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];



}
Run Code Online (Sandbox Code Playgroud)

我看了一下*NSInvalidUnarchiveOperationException,但不知道我应该如何在我的代码中实现它.我会感激任何例子.提前致谢!

iphone cocoa-touch objective-c nskeyedarchiver ios

7
推荐指数
1
解决办法
4789
查看次数

iCloud:如何读取用户创建的目录

我想在iCloud的Mobile Documents目录(在〜/ Library/Mobile Documents下的Lion中找到的目录)中由用户或应用程序创建的所有目录列表中读取.以下是此目录的示例:

iCloud的移动文档

我尝试了以下代码,但我运行的查询将不包含任何表示我的文件夹的对象(使用NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey).如果我运行txt文件的查询(使用@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey),我将分别为txt文件返回5个对象.因此寻找txt文件是有效的,但不适用于目录.通过阅读文档,我注意到Apple建议使用NSFileWrapper(文件包)而不是目录.iCloud无法处理/检测用户或应用程序创建的目录吗?

这是我的代码:

-(void)loadDocument {

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    _query = query;
    //Search all files in the Documents directories of the application’s iCloud container directories:
    [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]]; 

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey];

    [query setPredicate:pred];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
    [query startQuery];
}

- (void)queryDidFinishGathering:(NSNotification *)notification {

    NSMetadataQuery *query = [notification object];
    [query disableUpdates]; // You should invoke this …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nspredicate ios5 icloud

6
推荐指数
1
解决办法
1425
查看次数

UIDocument,NSFileWrapper和Images

我有一个UIDocument,我想由(1)一个txt文件和(2)几个jpg图像组成.我将txt和所有jpgs放入NSFileWrapper中.

当我加载UIDocument时,我需要txt文件中的信息非常快,所以我首先加载它并忽略所有图像,直到我真正需要它们.

虽然我知道如何懒洋洋地加载图像,但我不确定如何"懒洋洋地"保存图像(特别是在使用iCloud时,我不希望文件被不必要地上传/下载).假设我已经加载了所有图像并且没有更改它们.然后我想保存UIDocument,忽略所有图像(因为它们没有改变),但想要保存文本,因为它确实改变了.

我怎么做到这一点?它甚至可能吗?还是自动完成?或者我不应该将图像放在我的UIDocument中并让每个图像由不同的UIDocument处理?我害怕,这对我来说有点混乱.

到目前为止,这是我的代码,它将保存所有图像和文本(无论它们是否被更改):


UIDocument

-(id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {

        NSMutableDictionary *wrappers = [NSMutableDictionary dictionary];
// the following puts a wrapper into a dictionary of wrappers:
        [self encodeObject:self.text toWrappers:wrappers toFileName:@"text.data"];
        [self encodeObject:self.photos toWrappers:wrappers toFileName:@"photos.data"];
        NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];

        return fileWrapper;

    }
Run Code Online (Sandbox Code Playgroud)

当我想保存UIDocument时:

[self.doc saveToURL:self.doc.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
    [self.doc closeWithCompletionHandler:^(BOOL success) {}];
}];
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch nsfilewrapper icloud uidocument

6
推荐指数
1
解决办法
2511
查看次数

替换NSFileWrappers

我有一个NSFileWrapper目录,我想在其中更新某个FileWrapper.我想知道最好的方法是什么?

到目前为止,我已经使用了这段代码:

[self.fileWrapper addRegularFileWithContents:photoData 
                           preferredFilename:@"photo.data"];
Run Code Online (Sandbox Code Playgroud)

但是,只要FileWraper已经存在,我就会在FileWrapper中看到重复的内容,如下所示:

"1__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6bb0260>";
"2__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6b89b80>";
"3__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6ba1700>";
"4__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6bc8480>";
"photo.data" = "<NSFileWrapper: 0x6bcfc50>";
Run Code Online (Sandbox Code Playgroud)

如何防止这种情况并简单地替换FileWrapper - 在本例中为photo.data?我没有找到任何替换NSFileWrapper类参考中的FileWrappers的方法.

iphone cocoa objective-c nsfilewrapper

6
推荐指数
1
解决办法
852
查看次数

与iPhone DropBox API的简单同步

我对DropBox API感到有点沮丧.它应该是简单而直接的,但我还没有简单明了地解释如何进行简单的同步.

我按照DropBox API自述文件中的所有说明进行了操作.为了测试整个事情,我创建了两个按钮来下载文件或从我的DropBox上传文件.这些文件位于我的应用文档文件夹中.

这非常有效:

    -(void) DBupload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyExample.txt"];
    // NSError *error;
    [self.restClient uploadFile:@"MyExample.txt" toPath:@"/MyExamplePath" fromPath:filePath];
}

-(void) DBdownload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyExample.txt"];
    NSError *error;
    [self.restClient loadFile:@"/myExamplePath/MyExample.txt" intoPath:filePath];
}
Run Code Online (Sandbox Code Playgroud)

但是,我现在想知道如何实现简单的同步.现在,我可以手动上传和下载.但我需要同步的是:

  • 找出我的应用程序文件夹或我的DropBox文件夹中的MyExample.txt是否更新
  • 如果App的文件夹中的txt更新:将其放入dropbox(覆盖旧的),即调用我的DBupload方法
  • 如果下拉框中的txt更新:将其下载到apps文件夹中,即调用我的DBdownload方法

也许我只是太愚蠢了,但dropBox细节在某处如何实现这个相当简单和直接的任务?

我知道有这个,但它并没有给出任何代码示例.

谢谢你的任何建议.


编辑

好的,所以我想我的第一步是找出在dropBox中找到的MyExample.txt的最后修改日期.

我写了一个名为DBsync的奇妙方法,我只需输入以下命令:

 -(void) DBsync
{ …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c dropbox-api

5
推荐指数
1
解决办法
4703
查看次数

NSUserDefaults 与 NSKeyedArchiver

我认为这是关于数据管理的初学者问题。

我有一个应用程序可以加载和保存不同的 txt 文件。我想给每个 txt 文件一些没有保存在 txt 文件中的特征,例如名称、样式、颜色等。

到目前为止,我所做的是拥有一个 NSMutableArray *myLibrary 来保存所有这些引用。该数组如下所示:

  • 对于每个 txt 文件,一个对象被添加到 myLibrary
  • 每个对象包含几个 NSStrings、NSUInteger、BOOLs 和一些 NSMutableArrays
  • 在这些对象的子类中,我使用

    -(id)initWithCoder:(NSCoder *)aDecoder; -(void)encodeWithCoder:(NSCoder *)aCoder;

现在,我的大问题是最好和最安全的方法是坚持这些数据。到目前为止,我一直在使用 NSKeyedArchiver,但用户报告数据丢失,并且有一个奇怪的案例,据称 iTunes 同步导致数据混乱。NSUserDefaults 是否有任何保护程序?每次用户进行更改时,我都会保存 myLibrary.dat,所以我真的不明白为什么不应该 100% 保存。任何意见/建议/解释将不胜感激!

这是我编写和读取 myLibrary.Dat 的代码:

   -(NSString*)documentsDirectory
{
    NSLog(@"documentsDirectory");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    return documentsDirectory;
}

-(void)saveLibraryDat {

    NSLog(@"Saving myLibrary.dat...");
    NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"myLibrary.dat"];
    BOOL succeed = [[NSKeyedArchiver archivedDataWithRootObject:myLibrary] writeToFile:filePath atomically:YES];
    if (!succeed){ …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c data-structures

5
推荐指数
1
解决办法
2021
查看次数

自定义UINavigationController动画:CATransition

我有一个UINavigationController.我将VC1作为rootViewController并以编程方式从VC1加载VC2,然后让自定义动画从VC1转到VC2.标准.一切都很好,很好.

现在,我想在两者之间有一个自定义动画,如下所示: 自定义动画

总而言之,VC1在视图之外滑动,而VC2在其下方.就像一叠纸一样,你可以滑开第一张纸(VC1),从而露出下面的纸张(VC2).

所以我尝试的是以下代码,从VC1调用以获取VC2.但它有问题:

MyVC2 *vctwo = [[[MyVC2 alloc] init] autorelease];

CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

[[self navigationController] pushViewController:vctwo animated:YES];
Run Code Online (Sandbox Code Playgroud)

问题如下:

  1. VC2未在后台修复.它也有一些幻灯片(即使我指定了kCATransitionReveal).我想在后台完全修复VC2.
  2. VC1淡出.我真的不知道为什么.我没有使用kCATransitionFade之类的东西,所以我看不出淡入淡出的来源.

任何建议,为什么我没有得到预期的结果将非常感谢.对不起,如果这是显而易见的,但我现在正在尝试这几个小时,真的很沮丧.

iphone cocoa-touch core-animation uinavigationcontroller catransition

5
推荐指数
1
解决办法
2408
查看次数

如何监视本地文档目录NSMetaDataQuery

使用iCloud,我设置了一个MetaDataQuery,如下所示:

[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
Run Code Online (Sandbox Code Playgroud)

如何为我的普通本地文档目录设置相同的MetaDataQuery?

下面的代码将为我提供我目录中的静态文件列表 - 但我正在寻找一种更加动态的方式,以便我可以使用NSMetadataQueryDidUpdateNotification.

这是我在我的documentsDirectory中查找文件的静态代码:

NSArray* localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                           [self documentsDirectory] error:nil];

NSArray *onlyPQs = [localDocuments filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pq'"]];

NSLog(@"local file count: %i", [onlyPQs count]);

for (int i=0; i < [onlyPQs count]; i++) {
    NSLog(@"LOCAL:%@", [onlyPQs objectAtIndex:i]);
}
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c ios

5
推荐指数
1
解决办法
1316
查看次数

为什么 NSMetadataQueryDidUpdateNotification 被快速连续调用多次?

为了监控 iCloud 容器中的文件更改,我注册了

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(processiCloudUpdates:)
                                             name:NSMetadataQueryDidUpdateNotification
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

理想情况下,每当我收到已进行更新的消息时,我都会打开每个 UIDoc 并检查设置是否已更改。NSMetadataQueryDidUpdateNotification如果只调用一次就可以了。然而,它被触发了几次(我没有忘记等等removeObserver,并且有几个实例正在运行),因此每次触发更新通知时加载所有文件将变得非常不切实际(如果我有 5 个文件并且这被调用了 4 次,我会快速连续打开 20 个 UIDoc)。例如,如果我更新一个文件(并且我的无处不在的容器中只有一个文件),就会发生这种情况:

2012-07-05 10:51:39.565 Meernotes[7842:707] ... NSMetadataQuery update
2012-07-05 10:51:42.468 Meernotes[7842:707] ... NSMetadataQuery update
2012-07-05 10:51:45.216 Meernotes[7842:707] ... NSMetadataQuery update
2012-07-05 10:51:47.036 Meernotes[7842:707] ... NSMetadataQuery update
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以确定文件何时更改?NSFileVersion 也没有多大帮助。

ios icloud nsmetadataquery

5
推荐指数
1
解决办法
1901
查看次数

在iOS TestFlight设备上发生SIGSEGV崩溃

我有一个奇怪的崩溃,只发生在TestFlight ad hoc发行版上.我知道导致崩溃的原因(简单的循环,见下文),但我不明白为什么它不会在我的模拟器上崩溃,也不会在iPhone或iPad(带有开发配置文件)上崩溃.

我不断发布adhoc构建来追踪崩溃,但我终于放弃了.如何更有效地跟踪此错误?

这是TestFlight崩溃报告:

    Exception reason

SIGSEGV

Stacktrace

PRIMARY THREAD THREAD 0

0 Meernotes 0x0019649a testflight_backtrace + 158
1 Meernotes 0x001970c4 TFSignalHandler + 244
2 libsystem_c.dylib 0x32d9d7ec _sigtramp + 48
3 libobjc.A.dylib 0x37d34eac _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 224
4 libobjc.A.dylib 0x37d34eac _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 224
5 libobjc.A.dylib 0x37d34dc8 _objc_autoreleasePoolPop + 12
6 CoreFoundation 0x358e1cfe _CFAutoreleasePoolPop + 18
7 UIKit 0x333d5c92 _wrapRunLoopWithAutoreleasePoolHandler + 42
8 CoreFoundation 0x35965b1a __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
9 CoreFoundation 0x35963d56 __CFRunLoopDoObservers + 258
10 CoreFoundation 0x359640b0 __CFRunLoopRun + 760
11 …
Run Code Online (Sandbox Code Playgroud)

iphone segmentation-fault ad-hoc-distribution ios testflight

5
推荐指数
1
解决办法
738
查看次数