๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Application

[iOS] respondsToSelector, ์˜ต์…”๋„ ์ฒด์ด๋‹ ์‚ฌ์šฉํ•˜์—ฌ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์˜ ์„ ํƒ์  ๋ฉ”์†Œ๋“œ(optional method) ๊ตฌํ˜„ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ (in Swift, Objective-C)

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์˜ ์„ ํƒ์  ๋ฉ”์†Œ๋“œ(optional method) ๊ตฌํ˜„ ์—ฌ๋ถ€ ํ™•์ธํ•ด๋ณด์ž

 

 

Swift์™€ Objc์˜ ํ”„๋กœํ† ์ฝœ์—์„œ ๋ฉ”์†Œ๋“œ๋Š” ํ•„์ˆ˜, ์„ ํƒ์  ๋ฉ”์†Œ๋“œ๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

์„ ํƒ์  ๋ฉ”์†Œ๋“œ(optional method)๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „,

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ์„ ํƒ์ž์— ์‘๋‹ตํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.!!! 

๊ตฌํ˜„ํ•˜์ง€ ์•Š์€ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์‹œ null๊ฐ’์„ ๋ณด๋‚ด Crash๊ฐ€ ๋‚˜๊ธฐ ๋•Œ๋ฌธ!!! ๐Ÿ˜ญ

๋”ฐ๋ผ์„œ, ๋ฐ˜๋“œ์‹œ ํ”„๋กœํ† ์ฝœ์˜ ์˜ต์…”๋„ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์ „ ๊ตฌํ˜„ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผํ•œ๋‹ค. ๐Ÿ‘๐Ÿป

 

 


โ–ถ Objective-C ํ”„๋กœํ† ์ฝœ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ

 

์ผ๋‹จ ObjC ์—์„œ ํ”„๋กœํ† ์ฝœ์€ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด 

ํ‚ค์›Œ๋“œ๊ฐ€ ์—†๋Š” ๋ฉ”์†Œ๋“œ๋Š” ๋ชจ๋‘ ํ•„์ˆ˜์ ์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ,

@optional ํ‚ค์›Œ๋“œ ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ๋ชจ๋‘ ์„ ํƒ์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฉ”์†Œ๋“œ,

@optional ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ๋ฉ”์†Œ๋“œ๋“ค ๋’ค์— ๋‹ค์‹œ ํ•„์ˆ˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€๋กœ ์„ ์–ธํ•˜๋ ค๋ฉด @required ํ‚ค์›Œ๋“œ๋ฅผ ๋‹ค์‹œ ์จ์ค๋‹ˆ๋‹ค.

@protocol MyProtocol : NSObject
-(void)requiredMethod; 
@optional 
-(void)anOptionalMethod; 
@required 
-(void)anotherRequiredMethod; 
@end;

 

๊ทธ๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด @optional ๋กœ ์ •์˜๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด๋ด…์‹œ๋‹ค.

@protocol BViewControllerDelegate <NSObject>
@optional
-(void)sendMessage:(NSString*)message;
@end

 

๊ทธ๋ฆฌ๊ณ  ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๊ฐ€ ํŠน์ • ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค.

Objective-C์—์„œ๋Š” respondsToSelector: ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

respondsToSelector:
- (BOOL)respondsToSelector:(SEL)aSelector;

 

respondsToSelector ๋ฉ”์†Œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ์— @selector(ํ™•์ธํ•  ๋ฉ”์†Œ๋“œ) ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค!

๋ฉ”์†Œ๋“œ ๊ตฌํ˜„๋œ ๊ฒฝ์šฐ YES๊ฐ€ ์ถœ๋ ฅ, ์—†์œผ๋ฉด nil๊ฐ’์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

if (self.delegate && [self.delegate respondsToSelector:@selector(sendMessage:)]) {
	[self.delegate sendMessage: _messageField.text];
}

 


โ–ถ Swift ํ”„๋กœํ† ์ฝœ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ

 

์•„๋ž˜์™€ ๊ฐ™์ด Swift์—์„œ ์„ ํƒ์  ๋ฉ”์†Œ๋“œ๊ฐ€ ์ •์˜๋˜๋ฉด,

๋ฉ”์†Œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ํƒ€์ž…์ด ์ž๋™์œผ๋กœ ์˜ต์…”๋„์ด ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด, (Int)->Stringํƒ€์ž…์˜ ๋ฉ”์†Œ๋“œ๋Š” ((Int)->String)?์ด ๋ฉ๋‹ˆ๋‹ค.

์ „์ฒด ํ•จ์ˆ˜ ํƒ€์ž…์€ ํƒ€์ž…์˜ ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด๊ฐ’์ด ์•„๋‹Œ ์˜ต์…”๋„๋กœ ๋ž˜ํ•‘๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

@objc protocol BViewControllerDelegate {
    @objc optional func sendMessage(message: String)
}

 

๋”ฐ๋ผ์„œ, Swift์—์„œ๋Š” respondsToSelector:์™€ ๊ฐ™์€ ํŽธ๋ฆฌํ•œ ๋ฉ”ํƒ€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„๊ตฌ๊ฐ€ ์ง€์›๋˜์ง€ ์•Š์ง€๋งŒ,

์„ ํƒ์  ๋ฉ”์†Œ๋“œ๋ฅผ optional chaining์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ‘๐Ÿป

if((self.delegate) != nil){
  if let method = self.delegate?.sendMessage?(message: messageField.text!){
      method
  }
}

 

728x90
๋ฐ˜์‘ํ˜•