Lesson 3 Adding functionality to

2019-08-06  本文已影响0人  快乐捣蛋鬼

Master how to write and call methods in OC. Build a functional game.



1.Method Definition Syntax & Calling Methods

-(returnType)methodName: (parameterType*)firstParameter
            moMethodName:(parametherType*)secondParameter {
    body;
}
[receiver message: argument];
[[RPSTurn alloc] initWithMove: playersMove];

Sending a message to nil in OC is A-OK.(not method)
Just like we saw with properties, in order for a method to be visible outside of its class, it must be included in the header file.


2.Switch in OC

Switch statements in OC are quite limited compared to Swift, they can only condition upon the value of an integer.

switch (integer) {
    case 0:
        statement;
        break;
    case 1:
        statememt;
        break;
    default:
        statement;
        break 
}
switch (randomNumber) {
    case 0:
        return Rock;
        break;
    case 1:
        return Paper;
        break;
    case 2:
        return Scissors;
    default:
        return Invalid;
        break
}

3.if statement in OC

if (condition) {
    statements;
} else {
    statemetns;
}

Quiz: What is the principal difference between a function call and a message? How does messaging work in OC?

上一篇下一篇

猜你喜欢

热点阅读