开发者

Problem declaring and calling internal metthods

How do I declare and use small helper functions inside my normal methods ?

In on of my objective-c methods I need a function to find an item within a string

-(void) Onlookjson:(id) sender{
    NSString * res = [[sender gstring] copy];
    persInfoBirth.text = getKeyValue(res, @"Birth");
}

I came up with a normal C type declaration for helper function getKeyvalue like this

NSString * getKeyvalue(NSString * s, NSString * key){
   NSString *trm = [[s substringFromIndex:2] substringToIndex:[s length]-3];
   NSArray *list = [trm componentsSeparatedByString:@";"];
   //....
   NSString res;
      res = [list objectAtIndex:1];
   //...    
   re开发者_JAVA技巧turn res;
} 

Example input string in s:

s=@"{ Birth = "1910"; Death = "1936"; }";

Anyway I get an exception "unrecognized selector sent to instance" for any of the two first lines in the helper function

How do I declare helper functions that are just to be used internally and how to call them safely ?

regards

Martin


Is this the real code? Do you get zero errors and warnings from the compiler? You must not ignore compiler warnings and you should turn on the Static Analyser in addition to the standard warnings.

There are many things wrong with the above code, most of which are nothing todo with declaring and calling methods. There is no way the above code could compile so maybe it pasted incorrectly or something..

Anyway.. declaring and using methods. Why are using a c function? Unless you have a good reason why not use Objective-c ? If you do have a good reason to use a C function the your definition should be:-

NSString *getKeyvalue( NSString *s, NSString *key ){
  ...
}

note the arguments. As NSString instances reside in the heap (not on the stack) you always want to pass pointers to them.

You then need to put the declaration in the header file:-

NSString *getKeyvalue( NSString *s, NSString *key )

EDIT:

In Objective-c there is no distinction between normal methods and helper methods, there is only one kind, and you have aleray written one

- (void)onLookJson:(id)sender { .. }

Taking it apart..

  1. All methods begin with + or –, indicating Class method or Instance method. As you are familiar with C++ i guess you know what this means.

  2. (void) is the return type. ie this method doesn't return a value. If it did it might look like (float) or (NSString *) or (id).

  3. onLookJson: is the method name and the method takes 1 argument. Notice that the ':' is actually part of the name. This method is never is any circumstance just 'onLookJson'. An argument must always follow the :, so a method that doesn't take any arguments must not have one.

Ex

- (NSString *)fullName { .. }

This is an instance method, for example of a Person Class, you would call it like:-

NSString *theName = [aPerson fullName];

So

  • a method name that takes no arguments is like 'speak'

  • a method name that takes 1 argument is like 'speakTo:'

  • a method name that takes 2 arguments is like 'speakTo: language:'

  • a method name that takes 3 arguments is like 'speakTo: language: volume:'

etc.

All that is left is to put in the argument types and names. Your function definition:

NSString *getKeyvalue( NSString *s, NSString *key ){

would become..

- (NSString *)getValue:(NSString *)s key:(NSString *)key { .. }

again, you need to declare it in the header or you will get a compiler warning.

- (NSString *)getValue:(NSString *)s key:(NSString *)key;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜