레이블이 objective-c인 게시물을 표시합니다. 모든 게시물 표시
레이블이 objective-c인 게시물을 표시합니다. 모든 게시물 표시

2020년 4월 16일 목요일

Objective C로 만들어진 프로젝트에서 swift 사용하기

기존에 Objective C로 구현되어 있는 App 에 신규 기능을 추가 해야 하는데 swift로 하고 싶으면?


1. Xcode Project -> Target -> Build Settings -> Defines Modules 'Yes'
   Xcode Project -> Target -> Build Settings -> Swift Language Version 5







2. Swift Code 추가

3. Objective C 에서 swift 함수를 사용할 경우 @objc func , 프로퍼티를 참조 할 경우 @objc var

4. Swift에서 기존 Objective C 모듈에 연동이 필요한 경우
   프로젝트이름-Bridging-Header.h 에 #import 추가

이 과정을 거치고 나면 objective c / swift를 함께 이용할 수 있다.
ojbective c 에서 swift 로 만든 ViewController를 이용하고 swift로 만든 ViewController에서 기존 objective c 모듈에 기능을 이용 할 수 있다.




2015년 12월 2일 수요일

계층형에 내부 반복 (Array) 로 복잡한 json 문자열 생성하기

이번 미션에서는 어떠한 데이터를 받아서 json문자열을 생성 해야  한다.

지금 까지는 간단한 key :  value 형태의 json 문자열을 생성하여  WAS통신 하는 앱을 개발 했는데 그것은 간단한 NSArray 의 key value를 SBJson 모듈에 넣어서 생성 하면 간단 했다.

이번의 경우는

     timezone
     activity
        -- startTime
        -- stopTime
        -- calorie
        -- detail
            --startTime
            --stopTime
            --stepCnt
            -- distance
            -- mode
     sleep
        -- startTime
        -- stopTime
        -- calorie
        -- detail
            -- startTime
            -- stopTime
            -- settingstartTime
            -- settingstopTime
     

            mode


위와 같은 json 형태가 되는데 active 의 하위 key가 있고 그중 detail은 또 하위를 가지며
detal은 mode에 따라서 여러번 반복된다.
마찬가지로 sleep도 마찬가지이다.


    //sleep.detail
    NSMutableDictionary * sleep_detail1 = [[NSMutableDictionary alloc] init];
    [sleep_detail1 setValue:@"200" forKey:@"startTime"];
    [sleep_detail1 setValue:@"200" forKey:@"stopTime"];
    [sleep_detail1 setValue:@"200" forKey:@"settingstartTime"];
    [sleep_detail1 setValue:@"200" forKey:@"settingstopTime"];
    [sleep_detail1 setValue:@"Awake" forKey:@"mode"];

    NSMutableDictionary * sleep_detail2 = [[NSMutableDictionary alloc] init];
    [sleep_detail2 setValue:@"200" forKey:@"startTime"];
    [sleep_detail2 setValue:@"200" forKey:@"stopTime"];
    [sleep_detail2 setValue:@"200" forKey:@"settingstartTime"];
    [sleep_detail2 setValue:@"200" forKey:@"settingstopTime"];
    [sleep_detail2 setValue:@"Light Sleep" forKey:@"mode"];

    NSMutableDictionary * sleep_detail3 = [[NSMutableDictionary alloc] init];
    [sleep_detail3 setValue:@"200" forKey:@"startTime"];
    [sleep_detail3 setValue:@"200" forKey:@"stopTime"];
    [sleep_detail3 setValue:@"200" forKey:@"settingstartTime"];
    [sleep_detail3 setValue:@"200" forKey:@"settingstopTime"];
    [sleep_detail3 setValue:@"Deep Sleep" forKey:@"mode"];

    NSArray *sleep_detail_array = [NSArray arrayWithObjects: sleep_detail1,
                                   sleep_detail2,
                                   sleep_detail3,
                                   nil ];
    
    //sleep
    NSMutableDictionary * sleepData = [[NSMutableDictionary alloc] init];
    [sleepData setValue:@"200" forKey:@"startTime"];
    [sleepData setValue:@"200" forKey:@"stopTime"];
    [sleepData setValue:sleep_detail_array forKey:@"detail"];
    [sleepData setValue:@"200" forKey:@"calorie"];
    
    
    //activity.detail
    NSMutableDictionary * activity_detail1 = [[NSMutableDictionary alloc] init];
    [activity_detail1 setValue:@"200" forKey:@"startTime"];
    [activity_detail1 setValue:@"200" forKey:@"stopTime"];
    [activity_detail1 setValue:@"200" forKey:@"stepCnt"];
    [activity_detail1 setValue:@"200" forKey:@"distance"];
    [activity_detail1 setValue:@"Non-move" forKey:@"mode"];

    NSMutableDictionary * activity_detail2 = [[NSMutableDictionary alloc] init];
    [activity_detail2 setValue:@"200" forKey:@"startTime"];
    [activity_detail2 setValue:@"200" forKey:@"stopTime"];
    [activity_detail2 setValue:@"200" forKey:@"stepCnt"];
    [activity_detail2 setValue:@"200" forKey:@"distance"];
    [activity_detail2 setValue:@"Walking" forKey:@"mode"];

    NSMutableDictionary * activity_detail3 = [[NSMutableDictionary alloc] init];
    [activity_detail3 setValue:@"200" forKey:@"startTime"];
    [activity_detail3 setValue:@"200" forKey:@"stopTime"];
    [activity_detail3 setValue:@"200" forKey:@"stepCnt"];
    [activity_detail3 setValue:@"200" forKey:@"distance"];
    [activity_detail3 setValue:@"Fast Walking" forKey:@"mode"];

    NSMutableDictionary * activity_detail4 = [[NSMutableDictionary alloc] init];
    [activity_detail4 setValue:@"200" forKey:@"startTime"];
    [activity_detail4 setValue:@"200" forKey:@"stopTime"];
    [activity_detail4 setValue:@"200" forKey:@"stepCnt"];
    [activity_detail4 setValue:@"200" forKey:@"distance"];
    [activity_detail4 setValue:@"Running" forKey:@"mode"];

    NSArray *activity_detail_array = [NSArray arrayWithObjects: activity_detail1,
                                   activity_detail2,
                                   activity_detail3,
                                   activity_detail4,
                                   nil ];
    
    //activity
    NSMutableDictionary * activity = [[ NSMutableDictionary alloc] init];
    [activity setValue:@"200" forKey:@"statTime"];
    [activity setValue:@"200" forKey:@"stopTime"];
    [activity setValue:activity_detail_array forKey:@"detail"];
    [activity setValue:@"200" forKey:@"calorie"];
    
    //sync
    NSMutableDictionary * sync = [[ NSMutableDictionary alloc] init];
    [sync setValue:@"SEOUL" forKey:@"timezone"];
    [sync setValue:activity forKey:@"activity"];
    [sync setValue:sleepData forKey:@"sleep"];
    
    NSError *err;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sync options:NSJSONWritingPrettyPrinted error:&err];
    
    NSLog(@"JSON = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);


실제 디버깅 정보로 위에서 생성한 데이터가 json 문자열로 어떻게 생성되었는지 확인해보면
다음과 같다.
JSON = {
  "timezone" : "SEOUL",
  "activity" : {
    "stopTime" : "200",
    "detail" : [
      {
        "startTime" : "200",
        "stopTime" : "200",
        "stepCnt" : "200",
        "distance" : "200",
        "mode" : "Non-move"
      },
      {
        "startTime" : "200",
        "stopTime" : "200",
        "stepCnt" : "200",
        "distance" : "200",
        "mode" : "Walking"
      },
      {
        "startTime" : "200",
        "stopTime" : "200",
        "stepCnt" : "200",
        "distance" : "200",
        "mode" : "Fast Walking"
      },
      {
        "startTime" : "200",
        "stopTime" : "200",
        "stepCnt" : "200",
        "distance" : "200",
        "mode" : "Running"
      }
    ],
    "statTime" : "200",
    "calorie" : "200"
  },
  "sleep" : {
    "startTime" : "200",
    "stopTime" : "200",
    "detail" : [
      {
        "startTime" : "200",
        "stopTime" : "200",
        "settingstopTime" : "200",
        "settingstartTime" : "200",
        "mode" : "Awake"
      },
      {
        "startTime" : "200",
        "stopTime" : "200",
        "settingstopTime" : "200",
        "settingstartTime" : "200",
        "mode" : "Light Sleep"
      },
      {
        "startTime" : "200",
        "stopTime" : "200",
        "settingstopTime" : "200",
        "settingstartTime" : "200",
        "mode" : "Deep Sleep"
      }
    ],
    "calorie" : "200"
  }
}

2011년 7월 20일 수요일

wave파일을 효과음으로 내는 허접한 클래스


#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface SPSoundEffect : NSObject {
    
}
-(void) playWithWavFileName:(NSString *)fileName;

@end




#import "SPSoundEffect.h"


@implementation SPSoundEffect


- (void)playWithWavFileName:(NSString *)fileName
{
    NSString *fpath = [[NSBundlemainBundle] pathForResource:fileName ofType:@"wav"];
    SystemSoundID ssound;
    
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURLfileURLWithPath:fpath], &ssound);
    AudioServicesPlaySystemSound(ssound);
}

@end

2011년 7월 8일 금요일

Objective-C++ ??

Objective-C로 iOS프로그래밍을 한다.
그런데 Objective-C++ 이란것도 있다.

이게 먼고하니 간단히 이야기 해서 Objective-C에서 C언어를 혼재해서 쓸수 있는것처럼

Objective-C++은 C++을 혼재해서 쓰는 경우를 Objective-C++이라고 한다.

확장자는 .m이 아니라 .mm

C++과  Objective-C와의 클래스 상호간에 상속이 안된다는 제약은 있다.

하지만 C/C++ 로 만들어진 수많은 코드들을 이용하고 싶을때는 사용하면 좋을듯 하다.



언제나 모르는것 새로운것을 알게되는것은 참으로 흥미로운일이다.

2010년 12월 22일 수요일

Objective-C multiple argument to methods

-(int) set: (int) n: (int) d;

이렇게 선언 하고

[object set: 1 : 3];

이렇게 호출 할수도 있다.
(methods without argument names)

C/C++ 에서 여러 파라메터를 하나의 함수에 전달 하여 호출 할 때 이렇게 해야 할거 같은데...
1 : 3 ~~ 음 이게 좀 어색 하게 다가 온다.


오브젝티브C 에서는
일반적으로는 다음과 같이 선언 한다.

@interface Rectangle: NSObject
{
int width;
int height;
}

@property int width, height;
-(void) setWidth: (int) w andHeight: (int) h;

@implementation Rectangle;
-(void) setWidth: (int) w andHeight: (int) h
{
width = w;
height = h;
}

사용 할때
[myRect setWidth: 5 andHeight: 8];


2010년 12월 21일 화요일

Objective-C @property , @synthesize

@property , @synthesize 를 간단하게 요약해 봅시다.

@interface Fraction : NSObject
{
int numerator;
int denominator;
}

@property int numerator, denominator;

~~~
@end

-------------------------------------------------

@implementation Fraction

@synthesize numerator, denominator;

-------------------------------------------------

myFraction.numerator = 1;
myFraction.denomiator = 3;


요정도 까지..



2010년 12월 17일 금요일

Objective-C / C++keyword 비교

http://en.wikipedia.org/wiki/Objective-C
여기를 기준으로 간단하게 c++과 비교해보자
이걸 토대로 코드 컨버팅도 가능할듯 하다.

오브젝트의 메서드를 호출 하는 방법
obj->method(argument);

[obj method: argument];

class 선언

@interface classname : superclassname {     
// instance variables 
} 
+classMethod1; 
+(return_type)classMethod2;
+(return_type)classMethod3:(param1_type)param1_varName;   


-(return_type)instanceMethod1:(param1_type)param1_varName :(param2_type)param2_varName; 
-(return_type)instanceMethod2WithParameter:(param1_type)param1_varName andOtherParameter:(param2_type)param2_varName; 
@end


class classname : public superclassname {  
    public:   // instance variables     
    // Class (static) functions   
    static void* classMethod1();   
    static return_type classMethod2();   
    static return_type classMethod3(param1_type param1_varName);     
    // Instance (member) functions   
    return_type instanceMethod1(param1_type param1_varName, param2_type param2_varName);   
    return_type instanceMethod2WithParameter(param1_type param1_varName, param2_type param2_varName=default); 
};


-(void) setRangeStart:(int)start :(int)end; 
-(void) importDocumentWithName:(NSString *)name withSpecifiedPreferences:(Preferences *)prefs beforePage:(int)insertPage;


class 구현
@implementation classname 
+classMethod {     
    // implementation 
} -instanceMethod {     
    // implementation 
} 
@end




이거 말고 나머지
datatype 
걍 C/C++ 과 유사 하다고 할수 있음
 id 타입이란게 하나 있긴 한대 아직 모르겟음

operator 완전 같은거 같음

loop statement 완전 같다고 봄

making decisions (if / else / switch) 완전 같다고 봄

아직 나머지는 보지 못했음