我剛開始學習Objective-C,並試圖理解下面的代碼。有3個文件:FindLargestNumber.h,FindLargestNumber.m和main.m.Objective-C和@Interface文件中的數組
在FindLargestNumber.h文件中,我想知道是否有人能解釋我@interface NSMutableArray (FindLargestNumber)
的含義。我期待看到像@interface FindLargestNumber: NSObject
那樣的東西。
#import <Foundation/Foundation.h>
@interface NSMutableArray (FindLargestNumber)
- (id)largest:(NSMutableArray *)array;
@end
------------------------------------------------------------
#import "FindLargestNumber.h"
@implementation NSMutableArray (FindLargestNumber)
- (id)largest:(NSMutableArray *)array {
id currentLargest = @0;
for (id item in array) {
if (item > currentLargest) {
currentLargest = item;
}
}
return currentLargest;
}
@end
------------------------------------------------------
#import <Foundation/Foundation.h>
#import "FindLargestNumber.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *array = [NSMutableArray arrayWithObjects: @1, @4, @743, @77, @0, nil];
//NSLog(@"%@", array);
NSLog(@"The largest value is %@", [array largest:array]);
}
return 0;
}
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html – Kreiri
[OffTopic]其實找到一個數組的最大價值的最佳途徑是KVC:' NSArray * array = @ [@ 1,@ 4,@ 743,@ 77,@ 0];''NSLog(@「最大值爲%@」,[array valueForKeyPath:@「@ max.self」]); ' – vadian