-6

I need to pass and catch an NSString to an int returning method, how do I call it with passing the NSString, and in the method how do I catch that NSString?

- (IBAction)lower:(id)sender{
    NSString *finalcard = [self getCard];
    int *rank = 0; // [self getRank] < also how to pass the finalcard NSString to it?
}

- (NSString *)getCard{
    NSString *result = nil;

    return result;
}
- (int *)getRank{
    //if(passedString == @"randomcard"){return 1}else{  < how to catch the passedString?
    return 0;
    //}
}

In java passing would look like class.getRank("string"); and catching it would be in

static int getRank(String passedString){
donemanuel
  • 49
  • 1
  • 9
  • You should read Objective-C tutorials, it's not that easy as you think. The main diference is, when declaring methods in Objective-C, static ones are declared with a preceding "+", and instance method with a "-". Also the syntax is WAY too different. You won't get it "just seeing examples". – Alejandro Iván Jul 26 '13 at 20:57
  • 1
    First of all you should learn Objective-C. – Amin Negm-Awad Jul 26 '13 at 20:58
  • I think if anyone manages to answer this, most of the people new to Objective-C will understand it much better! – donemanuel Jul 26 '13 at 21:01
  • yes transform BUGGY Java code to Objective-C. I recommend to start from learning basics of both languages. – Marek R Jul 26 '13 at 21:02
  • Marek, Java is a bit easier to understand, thats why Im doing prototypes of my apps in Java... Also I know more than basic of Objective-C as I have my app on the AppStore, but I never used return methods by now when I need them! – donemanuel Jul 26 '13 at 21:05
  • On a side note `if(card == "randomcard")` will not work in Java. You should use the `.equals()` method to compare strings. – Kara Jul 26 '13 at 21:13
  • Klara I changed my question for all of you to understand easier what I want, also I found out myself how to do the calling methods and return NSString method, also == works in java, but .equals() is better :) – donemanuel Jul 26 '13 at 21:24
  • Objective C is no different or harder to use than Java. The syntax is just different or not what you're used to. – SevenBits Jul 26 '13 at 21:24
  • As an aside, `card == "randomcard"` will work *sometimes* in Java, but it's comparing to see if the references are the same, not whether the string contents are the same. Some Java implementations will intern *some* strings, so *sometimes* that line of code will work. (Objective-C exhibits similar properties.) – mipadi Jul 26 '13 at 21:27
  • 1
    Actually, in Objective-C, the == syntax compares memory locations of pointers, so this will never work. – SevenBits Jul 26 '13 at 21:35
  • It will only work sometimes. When you declare a literal NSString (@"some text"), you are assigning a memory space and if you use that @"some text" again, it will be the same NSString used before (the same memory address). That's done automatically by the compiler, because NSStrings are immutable and it will be safe to do it. That's why, if you do (@"some text" == @"some text") it will return YES. – Alejandro Iván Jul 26 '13 at 23:02
  • @SevenBits http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison in this question, the accepted answer gives a more accurate tip about this specific case. Happy coding! – Alejandro Iván Jul 28 '13 at 05:44

3 Answers3

0

Here you go (untested):

- (int)getRank:(NSString *passedString) {
    if([passedString isEqualToString:@"randomcard"]){
        return YES;
    } else {
        return NO;
    }
}

I suggest you learn Objective-C first. This is a very basic question.

SevenBits
  • 2,836
  • 1
  • 20
  • 33
0
+ (int)getRank:(NSString *)passedString
{
    return [passedString isEqualToString:@"randomcard"] ? 1 : 0;
}

I use the "+" symbol because you had it defined as "static" in your Java code and the equivalent in Objective-C are class methods.

So, if you call in Java:

int rank = Class.getRank("some string");

here in Objective-C you would do:

int rank = [Class getRank:@"some string"];

If you need to call this from an instance (an actual object), you could do:

int rank = [self.class getRank:@"some string"];
Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
-1

To begin, NSString (the iOS implementation of String) goes something like this:

NSString str = @"Some STR";

or

NSString = [NSString initWithFormat:@"Hello %@", "Hi there"]];
roguequery
  • 964
  • 13
  • 28
  • I would say the languages are quite a bit more different than you are implying – Paul Renton Jul 26 '13 at 21:00
  • 1
    Are you kidding me? Even method implementations differ. In which way "static void caller()" is mostly the same as "+ (void)caller"...? – Alejandro Iván Jul 26 '13 at 21:00
  • Ok, im not asking about NSString, i'm asking about methods... I know Objective-C fair enough, but methods are out of my reach – donemanuel Jul 26 '13 at 21:00
  • 1
    Methods are the crux of Objective-C... If you don't understand that, then you won't get very far, no matter what you learn here or no matter how much we try to teach you. – SevenBits Jul 26 '13 at 21:26
  • check out big nerd ranch, it's pretty good resource – roguequery Jul 27 '13 at 18:14