[iOS 강좌] 오픈API 이용한 날씨 APP 만들기 - 2. Objecive - C 기초

C 프로그래밍 언어에 스몰토크 스타일의 메시지 구문을 추가한 객체 지향 언어
원래는 넥스트의 NeXTSTEP 운영 체제에서 주 언어

NSLog


#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        int wheels =4;
        int seats =3;
        float cc =2.4;
        NSLog(@"wheels =%i",wheels);
        NSLog(@"seats =%i",seats);
        NSLog(@"version =%f",cc);
    }
    return 0;
}

@interface, @implementation

@interface Car : NSObject

{
  int wheels;
  int seats;
}
-(void)setWheels:(int)w;
-(void)setSeats:(int)s;
@end
@implementation Car
@end


alloc, init

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        Car *car =[[Car alloc] init];
        
        [car setSeats:4];
        [car setWheels:5];
        [car print];
        
    }
    return 0;
}

#import  

#import "Car.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        Car *car =[[Car alloc] init];
        
        [car setSeats:4];
        [car setWheels:5];
        [car print];
        
    }
    return 0;
}

@property, @synthesize
@property(프로퍼티), @ synthesize(신디사이즈)
다른 클래스에서 클래스 내 변수를 사용할 수 있게 getter, setter 접근자를 메서드를 자동으로 생성해 주는 지시어

@interface Biycle : NSObject
{
    int wheels;
    int seats;
}
@property int wheels;
@property int seats;
-(void)print;
@end
@implementation Biycle
@synthesize wheels,seats;
@end


instance method, class method

메서드(-) : 인스턴스 메서드로 public 메서드와 동일한 기능을 가짐.
메서드(+) : 클래스 메서드로 static 메서드와 동일한 기능을 가짐.
반환값(void) : 아무것도 반환하지 않을때
반환값(int) : 메서드를 선언할때 메서드 실행 후 어떤형으로 반환할 지
#import <Foundation/Foundation.h>
@interface Bike : NSObject
@property int wheels;
@property int seats;
+(void)sound;
-(void)print;
-(void)setWheels:(int)w Seats:(int)s;
-(int)getWheels;
@end

NSString, NSMutableString (문자열, 변경가능한 문자열)

NSString *str =[[NSString alloc] init];
- (NSString *)substringFromIndex:(NSUInteger)from; // 부터
- (NSString *)substringToIndex:(NSUInteger)to; // 까지
- (NSString *)substringWithRange:(NSRange)range; 
       
NSMutableString *mStr =[[NSMutableString alloc] initWithString:str];
- (void)appendString:(NSString *)aString;
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
- (void)deleteCharactersInRange:(NSRange)range;

NSArray, NSMutableArray (배열, 변경가능한 배열)

NSArray *array =[NSArray arrayWithObjects:@"1",@"2", nil];
NSArray *array1 =[[NSArray alloc] initWithObjects:@"1",@"2", nil];
NSArray *array2 =@[@"1",@"2"];
[array2 count] -> 갯수
NSMutableArray *mArray =[[NSMutableArray alloc] initWithArray:array];
- (void)addObject:(ObjectType)anObject;
- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject;

- (void)removeAllObjects;


NSDictionary, NSMutableDictionary (딕셔너리, 변경가능한 딕셔너리 - 사전객체)

NSDictionary *dic =[[NSDictionary alloc] initWithObjectsAndKeys:@"19",@"age",@"lee",@"name", nil];
NSDictionary *dic2 =@{@"age":@"20",@"name":@"park"};
NSLog(@"age = %@",[dic objectForKey:@"age"]);
NSLog(@"name = %@",[dic objectForKey:@"name"]);
NSMutableDictionary *mDic =[[NSMutableDictionary alloc] initWithDictionary:dic];
[mDic setObject:@"35" forKey:@"age"];
[mDic setObject:@"kim" forKey:@"name"];
        
NSLog(@"age = %@",[mDic objectForKey:@"age"]);
NSLog(@"name = %@",[mDic objectForKey:@"name"]);

댓글

주간 인기글

카드뉴스 마케팅 팁

[ubuntu] 신규 계정에 sudo 권한 추가하기

[Spring Boot] JPA + Pageable 을 이용한 페이징 처리

[정보] 인스타그램은 당신의 소리를 '듣고' 있을 수도 있습니다

HTML 템플릿을 인쇄용으로 가공하기