[Code]Objective-C基礎語法Example

  • 2577
  • 0
  • 2016-03-05

摘要:[Objective-C]基礎語法Example

這篇筆記只是單純的把TutorialsPoint的Objective-C教學的Example摘取下來,若想要了解更多訊息,可連至該網站-TutorialsPoint的Objective-C教學。個人覺得還滿簡單清楚的。

基本Syntax

宣告Variable

Example

/* variable definition: */
int a, b;
int c;
float f;

/* actual initialization */
a = 10;
b = 20;

宣告Constants

Example

const int  LENGTH = 10;
const int  WIDTH  = 5;
const char NEWLINE = '\n';

for迴圈

Example

int a;
for( a = 10; a < 20; a = a + 1 )
{
   NSLog(@"value of a: %d\n", a);
}

while迴圈

Example

int a = 10;
while( a < 20 )
{
    NSLog(@"value of a: %d\n", a);
    a++;
}

switch

Example

char grade = 'B';

switch(grade)
{
case 'A' :
  NSLog(@"Excellent!\n" );
  break;
case 'B' :
case 'C' :
  NSLog(@"Well done\n" );
  break;
case 'D' :
  NSLog(@"You passed\n" );
  break;
case 'F' :
  NSLog(@"Better try again\n" );
  break;
default :
  NSLog(@"Invalid grade\n" );
}

Function

Example-Function定義

- (int) max:(int) num1 secondNumber:(int) num2 
{
   /* local variable declaration */
   int result;

   if (num1 > num2)
   {
      result = num1;
   }
   else
   {
      result = num2;
   }

   return result; 
}

Example-Function叫用

   int a = 100;
   int b = 200;
   int ret;

   SampleClass *sampleClass = [[SampleClass alloc]init];

   /* calling a method to get max value */
   ret = [sampleClass max:a andNum2:b];

Array

Example-宣告陣列

double balance[10];

Example-初始化陣列

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

Block

Block語法

returntype (^blockName)(argumentType);

Example

void (^simpleBlock)(void) = ^{
    NSLog(@"This is a block");
};

帶參數與回傳值

double (^multiplyTwoValues)(double, double) = 
    ^(double firstValue, double secondValue) {
        return firstValue * secondValue;
    };
double result = multiplyTwoValues(2,4); 
NSLog(@"The result is %f", result);

物件導向Syntax

Object & Class

Example

#import <Foundation/Foundation.h>

@interface SampleClass:NSObject
- (void)sampleMethod;
+ (void)sampleClassMethod;
@end

@implementation SampleClass

- (void)sampleMethod{
    NSLog(@"Hello, World! \n");
}

+ (void)sampleClassMethod{
    NSLog(@"class(static) method!");
}
@end

int main()
{    
    SampleClass *sampleClassObj = [[SampleClass alloc]init];
    [sampleClassObj sampleMethod];
    [SampleClass sampleClassMethod];
    return 0;
}

屬性

Example

@interface Person : NSObjects
    @property(nonatomic, readwrite) double height;
@end


@implementation Person
    @synthesize height;
@end

Protocol

Syntax-宣告Protocol

@protocol ProtocolName
@required
// list of required methods
@optional
// list of optional methods
@end

Example-宣告Protocol

@protocol PrintProtocolDelegate
- (void)processCompleted;
@end

Syntax-採用Protocol

@interface MyClass : NSObject <MyProtocol>
...
@end

Example-採用Protocol

@interface SampleClass:NSObject<PrintProtocolDelegate>
@end

@implementation SampleClass
-(void)processCompleted{
    NSLog(@"Printing Process Completed");
}
@end

Category

Syntax

@interface ClassName (CategoryName)

@end

Example

@interface NSString(MyAdditions)
+(NSString *)getCopyRightString;
@end

@implementation NSString(MyAdditions)
+(NSString *)getGreetString{
    return @"Hello";
}
@end

int main(int argc, const char * argv[])
{

   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSString *greeting = [NSString getGreetString];
   NSLog(@"Accessing Category: %@",greeting);
   [pool drain];
    return 0;
}

s