๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Programming ๊ฐœ๋ฐœ/๐ŸŽ iOS ๊ฐœ๋ฐœ, Swift

[Objective-C] .h์™€ .m ํŒŒ์ผ์˜ ์—ฐ๊ฒฐ์„ฑ

by kimdee 2023. 1. 30.
๋ฐ˜์‘ํ˜•

 

 

 


 

Xcode์—์„œ obj-C ํ”„๋กœ์ ํŠธ๊ฐ„์˜ ์—ฐ๊ฒฐ์„ฑ๊ณผ ์”ฌ ๋’ค์— ์ˆจ๊ฒจ์ง„ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ํด๋ž˜์Šค์˜ public์ด๋ž‘ privateํ•œ ํŒŒํŠธ๋ฅผ ๋‚˜๋ˆ„๋Š”๋ฐ ์‚ฌ์šฉ์ด ๋จ. .h๋Š” ํ—ค๋” ํŒŒ์ผ๋กœ ๋งˆ์น˜ API์ฒ˜๋Ÿผ ํด๋ž˜์Šค์˜ public ์„ ์–ธ์„ ํ•˜๋Š” ํŒŒ์ผ์ด๊ณ , .mํŒŒ์ผ์€ ๋น„๊ณต๊ฐœ๋˜๋Š” ๊ตฌํ˜„์„ ํ•จ.

 

๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ฐธ์กฐ๋ฅผ ์œ„ํ•ด .h ํŒŒ์ผ์„ import ํ•ด์™€์•ผ ํ•œ๋‹ค.

 

#import <Foundation/Foundation.h>

 

.h ํŒŒ์ผ์—์„œ public @property๋ฅผ ํด๋ž˜์Šค์— ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Œ.

 

@property (strong, nonatomic) NSString *something;

 

์ด @property๋Š” NSString ํด๋ž˜์Šค ์˜ค๋ธŒ์ ํŠธ์˜ ํฌ์ธํ„ฐ. ๋ชจ๋“  ์˜ค๋ธŒ์ ํŠธ๋Š” ํž™์— ์žˆ์œผ๋ฏ€๋กœ ์ด ์žˆ์–ด์•ผํ•จ. ๋ณ„๊ฐœ๋กœ ์ด ์˜ ๋œป์€ ์˜ค๋ธŒ์ ํŠธ ํฌ์ธํŠธ๋ฅผ ํ”„๋กœํผํ‹ฐ๊ฐ€ nil๋กœ ์„ค์ •๋˜๊ธฐ ์ „๊นŒ์ง€ ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€ํ•˜๋ผ๋Š” ๋œป. nonatomic์€ ์ด ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์Šค๋ ˆ๋“œ ์„ธ์ดํ”„ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ. nonatomic์ด ์•„๋‹ˆ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ฝ”๋“œ ์ž ๊ธˆ์„ ๋งŒ๋“ค ๊ฒƒ.

 

.m ํŒŒ์ผ์€ ์ด ํ”„๋กœํผํ‹ฐ์˜ ๊ฒŒํ„ฐ์™€ ์„ธํ„ฐ ๋ฉ”์„œ๋“œ๊ฐ€ ์”ฌ ๋’ค์—์„œ ์ž๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด์„œ @property์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ฌ.

 

@synthsize something = _somthing;
- (NSString *) something
{
  return _something;
}
- (void)setSomething:(NSString *)something
{
  _something = something;
}

 

๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›(backing) ๋ณ€์ˆ˜ ์ด๋ฆ„์€ ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„๊ณผ ๋™์ผํ•˜๊ณ  ์•ž์— ์–ธ๋”์Šค์ฝ”์–ด(_)๋ฅผ ๋ถ™์ธ ํ˜•ํƒœ. ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•ด์„œ ๋‹ค๋ฅธ ๊ฑธ ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ์œ„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š”๋Š” ์—†์Œ.

 

์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” .h ํŒŒ์ผ ์•ˆ์— ์„ ์–ธ์„ ๋‘์–ด์•ผํ•จ.

 

- (int)newMethod:(ArgType *)arg;

๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ ์ž์„ธํ•œ ๋ถ€๋ถ„์€ .m ํŒŒ์ผ์— ์ž‘์„ฑ.

 

- (int)newMethod:(ArgType *)arg
{
  int num = 0;
  # something in the method...
  return num;
}

๋น„๊ณต๊ฐœ๋กœ ์„ ์–ธํ•  ๋•Œ .m ํŒŒ์ผ ์•ˆ์— ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑ.

 

@interface Something()
#private declarations....
@end

๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์ฒ˜์Œ์œผ๋กœ ์ฝ์„ ๋•Œ .h ํŒŒ์ผ์„ ๋ณด๊ณ  ํ”„๋กœ์ ํŠธ์˜ ๊ฐœ์š”๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Œ. ์ž์„ธํ•˜๊ฒŒ ๋ณด๋ ค๋ฉด .m ํŒŒ์ผ.

 


๋ณธ ๊ธ€์€ ์•„๋ž˜ ๊ธ€์˜ ๋ฒˆ์—ญ, ์š”์•ฝ ๊ธ€์ž…๋‹ˆ๋‹ค.

* ๊ฐœ์ธ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ์— ์˜ฌ๋ฆฐ ๋‚ด์šฉ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 

 

 

Connection between .h and .m files in Objective-C

When first open an objective-C project in X-code, the .h and .m files look confusing. It is important to understand the simple connections…

victorleungtw.medium.com

 


 

 

์˜ค๋Š˜๋„ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. 

 

๊ถ๊ธˆํ•˜๊ฑฐ๋‚˜ ๋‚˜๋ˆ„๊ณ  ์‹ถ์€ ์–˜๊ธฐ๊ฐ€ ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

์žฌ๋ฐŒ๊ฒŒ ์ฝ์œผ์…จ๋‹ค๋ฉด ๊ณต๊ฐ๊ณผ ๊ตฌ๋…์€ ํฐ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค. 

 

ํ•ญ์ƒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€