当试图覆盖nserror无法得到任何东西

时间:2014-09-28 00:33:41

标签: objective-c

我想根据nserror信息做,创建一个新的nserror实例,但似乎根本不起作用

    - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError * *)outError 
{

    NSError *breakError = nil;

    todoItems = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:NULL error:&breakError]; 
    if (todoItems == nil){
        NSString *desc = NSLocalizedString(@"Can't do it!", @"");
        NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
        *outError = [NSError errorWithDomain:@"com.pink.test" code:3084 userInfo:userInfo];
        return NO;
    }


    // Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
    // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    // If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return YES;
}

我总是只能得到默认的对话框消息,即使我没有传回nserror enter image description here


对不起,如果不清楚的话,这是一个演示,我会重复发生的事情。

enter image description here enter image description here enter image description here enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

加油了。你想陈述的问题,跟别人看到的可能不一样。

如果在该函数中有错误输出,则无需抛出异常。

以下是从kxsmb:

制作nserror的示例
static NSError * mkKxSMBError(KxSMBError error, NSString *format, ...)
{
    NSDictionary *userInfo = nil;
    NSString *reason = nil;

    if (format) {

        va_list args;
        va_start(args, format);
        reason = [[NSString alloc] initWithFormat:format arguments:args];
        va_end(args);
    }

    if (reason) {

        userInfo = @{
                     NSLocalizedDescriptionKey : KxSMBErrorMessage(error),
                     NSLocalizedFailureReasonErrorKey : reason
                     };

    } else {

        userInfo = @{ NSLocalizedDescriptionKey : KxSMBErrorMessage(error) };
    }

    return [NSError errorWithDomain:KxSMBErrorDomain
                               code:error
                           userInfo:userInfo];
}

答案 1 :(得分:0)

它完成了,结果它只能使用系统定义错误,不能自定义,它意味着只能是errorWithDomain:NSCocoaErrorDomain和有效代码

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError * *)outError
{
    if (*outError == nil){
        NSString *desc = NSLocalizedString(@"Can't do it!", @"");
        NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
        *outError = [NSError errorWithDomain:NSCocoaErrorDomain code:3840 userInfo:userInfo];
    }

    return NO;

感谢所有人的帮助。