いつもお世話になっております。
表題の件ですが、他クラスを記述したファイルを作成してそれを#importするときや.h,.m,両方できると思います。
そこで、.hで読み込む時と.mで読み込む時のそれぞれの利点やどっちのファイルで読み込ませるかを決める理由を教えてください。
また、delegateを使う時に.hに記述してある@interface ViewController : UIViewControllerの後に<delegate名>だったり.mの@interface ViewController ()<delegate名>こちらも両方同じ意味だと思っているのですが、
この場合も使い分ける時どっちにするかを決める理由等も教えて下さい。
以上です。
よろしくお願いいたします。
*必要ないと思いますがサンプルのソースコードを載せておきます。
Square.h
objective
1@interface Square : NSObject{ 2 @public 3 int width; 4 int height; 5} 6-(int)getArea; 7@end
Square.m
objective
1#import "Square.h" 2 3@implementation Square 4-(int)getArea{ 5 6 int area; 7 width = 10; 8 height = 2; 9 area = width * height; 10 11 return area; 12} 13@end
ViewController.h
objective
1#import <UIKit/UIKit.h> 2#import "Square.h" //.hで読み込む時 3 4@interface ViewController : UIViewController<delegateを使う時ここにもかける> 5@end
ViewController.m
objective
1#import "ViewController.h" 2#import "Square.h" //.mで読み込む時 3 4@interface ViewController ()<delegateを使う時ここにもかける> 5 6@end 7 8@implementation ViewController 9 10- (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 Square *square = [[Square alloc]init]; 14 [square getArea]; 15 NSLog(@"square:%d",[square getArea]); 16 17} 18 19- (void)didReceiveMemoryWarning { 20 [super didReceiveMemoryWarning]; 21 // Dispose of any resources that can be recreated. 22} 23 24@end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/02 04:01