简体   繁体   中英

Passing NSString objects between two class methods

I am just learning Objective-C, in Xcode 4.2. What I am trying to do should be very simple, but it is driving me nuts. I am trying to pass an NSString object from one Class Method to another Class Method, and I have been able to successfully pass NSArray's.

I have looked around, and found questions on how to pass NSString objects between two Instance Methods (which I can get to work fine). The two class's are below:

+ (double)numberOfOperations:(NSArray *)program equalityTest:(NSString *)testForEquality
{
    NSSet *stackSet = [[NSSet alloc] initWithArray:program];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:testForEquality];
    NSSet *filteredStackSet = [stackSet filteredSetUsingPredicate:predicate];
    return [filteredStackSet count];

}

+ (double)descriptionOfProgram:(NSMutableArray *)program
{
    NSArray *stack = [program copy];

    double test = [self numberOfOperations:stack equalityTest:@"+"];
    return test;
}

when I compile, and run the above code, i get an error at run time "Unable to parse string "+". I have also pasted the debbuger code below:

2011-12-27 14:51:53.755 Calculator[320:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "+"'
* First throw call stack:
(0x13be052 0x154fd0a 0x94fdc1 0x94fbdf 0x94fb93 0x46fb 0x4829 0x3c9c 0x2ac3 0x2a3e 0x13bfec9 0x185c2 0x1855a 0xbdb76 0xbe03f 0xbd2fe 0x3da30 0x3dc56 0x24384 0x17aa9 0x12a8fa9 0x13921c5 0x12f7022 0x12f590a 0x12f4db4 0x12f4ccb 0x12a7879 0x12a793e 0x15a9b 0x1f38 0x1e95) terminate called throwing an exception(gdb).

Is it even possible to pass NSString objects between classes? Let me know if you need more information.

@"+" is not a valid predicate format, it's spitting the dummy because it is unable to create an NSPredicate object with the specified format. It's not to do with passing strings around.

Have a quick once-over of the Predicate Programming Guide to see what a predicate string should look like.

The problem isn't with passing strings, that is working fine. The problem is with the predicate. "+" is not a valid predicate, it doesn't make any sense. Please see the Predicate Programming Guide for more information on creating predicates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM