将自定义对象导入多个文件
Importing custom objects into multiple files
我在将自定义对象导入我的 viewController 时遇到了一些问题。
我已经创建了一个消息对象,其中包含来自服务器的 json 响应。
然后我创建了一个 MessageManager class,它执行对 API.
的所有网络调用
MessageManager 导入 Message,以创建消息对象并将其传递给 class 使用它的任何对象。
我现在有我的 viewController class,它#import MessageManager 和 Message。所以我可以创建 Message 的实例变量。
但是我 运行 编译器出现重复符号错误。
如何解决这个问题,并获得正确的导入顺序。
如果我从 viewcontroller 中删除 Message.h,并删除任何引用 Message 的代码,它会正常编译。
Messages.h 由 JSONModel 返回并解析 json object
#import <JSONModel/JSONModel.h>
@protocol Messages;
@interface Messages : JSONModel
@property (nonatomic) NSString<Optional>* id;
@property (nonatomic) NSString<Optional>* sender_id;
@property (nonatomic) NSString<Optional>* receiver_id;
@property (nonatomic) NSString<Optional>* user_user_id;
@property (nonatomic) NSString<Optional>* job_id;
@property (nonatomic) NSString<Optional>* file_id;
@property (nonatomic) NSString<Optional>* title;
@property (nonatomic) NSString<Optional>* replied;
@end
@protocol MessagesModel;
@interface MessagesModel : JSONModel
@property (nonatomic) NSString<Optional>* action;
@property (nonatomic) NSDate<Optional>* dateTime;
@property (nonatomic) NSString<Optional>* result;
@property (nonatomic) NSString<Optional>* numItems;
@property (nonatomic) NSArray <Messages> *items;
@end
@implementation MessagesModel
+ (JSONKeyMapper *)keyMapper
{
return [JSONKeyMapper mapperForSnakeCase];
}
@end
MessageViewController.h
#import <UIKit/UIKit.h>
@interface MessagesViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UISearchController *searchController;
@end
MessageViewController.m
#import "MessagesViewController.h"
#import "MessageTableViewCell.h"
#import "MessageThreadViewController.h"
#import "Messages.h" <--Need Messages Object to use Object and pass object around
#import "MessageManager.h"
@interface MessagesViewController () <UISearchBarDelegate, UISearchResultsUpdating, UIActionSheetDelegate, UISearchControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *inbox;
@property (strong, nonatomic) NSArray *sent;
@property (strong, nonatomic) NSArray *deleted;
@property (strong, nonatomic) NSArray *temp;
@property (assign, nonatomic) NSInteger selectedBox;
@property (assign, nonatomic) NSInteger selectedMessage;
@property (strong, nonatomic) MessageManager *messageManager;
@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
@property (strong, nonatomic) Messages *messages;
@end
MessageManager.h
#import <Foundation/Foundation.h>
@interface MessageManager : NSObject
- (void)getReceivedMessagesListForReceiverID:(NSString*)receiverID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)getSentMessagesListForSenderID:(NSString*)senderID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)getDeletedMessagesListForMemberID:(NSString*)memberID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchReceivedMessagesListForReceiverID:(NSString*)receuverID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchDeletedMessagesListForMemberID:(NSString*)memberID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)unreadMessagesCountForReceiverID:(NSString*)receiverID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)sendMessageToReceiverID:(NSString*)receieverID ToSenderID:(NSString*)senderID withTitle:(NSString*)title andMessage:(NSString*)message success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)markMessageRead:(NSString*)messageID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)markMessageUnRead:(NSString*)messageID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)receivedListCountForReceivedID:(NSString*)receivedID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)deletedMessagesListCountForMemberID:(NSString*)memberID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchReceivedListCountForReceiverID:(NSString*)receiverID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchSentListCountForSender:(NSString*)senderID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchDeletedListCountForMemberID:(NSString*)memberID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
@end
MessageManager.m
#import "MessageManager.h"
#import "Constants.h"
#import <AFNetworking/AFNetworking.h>
#import "Messages.h" <--Need Messages Object to create Messages from response
@implementation MessageManager
...
错误
duplicate symbol _OBJC_IVAR_$_MessagesModel._result in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_MessagesModel._items in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_MessagesModel._numItems in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_CLASS_$_Messages in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_METACLASS_$_Messages in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_Messages._deleted_by_sender in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_MessagesModel._action in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_Messages._receiver_login in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_Messages._sender_login in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
...
根据您在问题中提到的文件,您的项目中只有 Messages.h
文件而没有 Messages.m
文件,而是在 header 文件,我说的对吗?通过从您的问题中复制 classes,我能够在示例项目 I found here 上复制您的重复符号错误。但是当我创建一个 Messages.m 文件同时实现 Messages
和 MessageManager
classes 时,重复符号编译器错误消失了,我能够创建一个实例MessageManager
class.
话虽如此,我不确定您所说的
是什么意思
The JSONModel tutorials all show doing the @implementation in the header, and not needing the .m files at all.
评论,但根据 JSONModel 的 Github readme page,他们说:
There's no need to do anything in the implementation (.m) file.
这可能并不意味着您不需要 .m 文件。无论如何,为 class 同时拥有 .h 和 .m 文件是一个好习惯,因为 Xcode 项目目标的 Build Phases
选项卡下的 Compile Sources
选项只有 .h 和 .m 文件。 m 个文件。这就是告诉编译器在构建目标时应该编译哪些源文件。由于您在 .h 文件中声明了实现,因此编译器在构建时无法找到它。
这是我设置的示例 Xcode 项目的屏幕截图:
MessageManager.h 文件
#import <Foundation/Foundation.h>
@class MessagesModel;
@interface MessageManager : NSObject
+ (MessagesModel *)getMessages;
@end
MessageManager.m 文件:
#import "MessageManager.h"
#import "Messages.h" //<-- Imported here
@implementation MessageManager
+ (MessagesModel *)getMessages {
NSError *error = nil;
NSDictionary *dictionary = @{@"action": @"replied",
@"dateTime": @"",
@"result": @"success",
@"numItems": @"1",
@"items": @[@{@"id": @"1",
@"sender_id": @"2",
@"receiver_id": @"3",
@"user_user_id": @"4",
@"job_id": @"5",
@"file_id": @"6",
@"title": @"Hello world",
@"replied" : @"7"}]};
MessagesModel *messages = [[MessagesModel alloc] initWithDictionary:dictionary error:&error];
if (error)
NSLog(@"Oops!");
return messages;
}
@end
Messages.h 文件:
#import <JSONModel/JSONModel.h>
@protocol Messages;
@interface Messages : JSONModel
@property (nonatomic) NSString<Optional>* id;
@property (nonatomic) NSString<Optional>* sender_id;
@property (nonatomic) NSString<Optional>* receiver_id;
@property (nonatomic) NSString<Optional>* user_user_id;
@property (nonatomic) NSString<Optional>* job_id;
@property (nonatomic) NSString<Optional>* file_id;
@property (nonatomic) NSString<Optional>* title;
@property (nonatomic) NSString<Optional>* replied;
@end
@protocol MessagesModel;
@interface MessagesModel : JSONModel
@property (nonatomic) NSString<Optional>* action;
@property (nonatomic) NSDate<Optional>* dateTime;
@property (nonatomic) NSString<Optional>* result;
@property (nonatomic) NSString<Optional>* numItems;
@property (nonatomic, strong) NSArray <Messages> *items;
@end
Messages.m 文件:
#import "Messages.h"
@implementation Messages
@end
@implementation MessagesModel
+ (JSONKeyMapper *)keyMapper {
return [JSONKeyMapper mapperForSnakeCase];
}
@end
ViewController.m 文件:
#import "ViewController.h"
#import "MessageManager.h"
#import "Messages.h" //<-- Imported here
@interface ViewController ()
@property (strong, nonatomic) MessagesModel *messagesModel;
@property (weak, nonatomic) NSArray<Messages *> *messages;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.messagesModel = [MessageManager getMessages];
self.messages = self.messagesModel.items;
NSLog(@"%@", self.messages);
}
@end
如果您希望我上传 Xcode 项目,请告诉我,以备您检查设置时使用。
希望这对您有所帮助。
我在将自定义对象导入我的 viewController 时遇到了一些问题。
我已经创建了一个消息对象,其中包含来自服务器的 json 响应。 然后我创建了一个 MessageManager class,它执行对 API.
的所有网络调用MessageManager 导入 Message,以创建消息对象并将其传递给 class 使用它的任何对象。
我现在有我的 viewController class,它#import MessageManager 和 Message。所以我可以创建 Message 的实例变量。 但是我 运行 编译器出现重复符号错误。
如何解决这个问题,并获得正确的导入顺序。
如果我从 viewcontroller 中删除 Message.h,并删除任何引用 Message 的代码,它会正常编译。
Messages.h 由 JSONModel 返回并解析 json object
#import <JSONModel/JSONModel.h>
@protocol Messages;
@interface Messages : JSONModel
@property (nonatomic) NSString<Optional>* id;
@property (nonatomic) NSString<Optional>* sender_id;
@property (nonatomic) NSString<Optional>* receiver_id;
@property (nonatomic) NSString<Optional>* user_user_id;
@property (nonatomic) NSString<Optional>* job_id;
@property (nonatomic) NSString<Optional>* file_id;
@property (nonatomic) NSString<Optional>* title;
@property (nonatomic) NSString<Optional>* replied;
@end
@protocol MessagesModel;
@interface MessagesModel : JSONModel
@property (nonatomic) NSString<Optional>* action;
@property (nonatomic) NSDate<Optional>* dateTime;
@property (nonatomic) NSString<Optional>* result;
@property (nonatomic) NSString<Optional>* numItems;
@property (nonatomic) NSArray <Messages> *items;
@end
@implementation MessagesModel
+ (JSONKeyMapper *)keyMapper
{
return [JSONKeyMapper mapperForSnakeCase];
}
@end
MessageViewController.h
#import <UIKit/UIKit.h>
@interface MessagesViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UISearchController *searchController;
@end
MessageViewController.m
#import "MessagesViewController.h"
#import "MessageTableViewCell.h"
#import "MessageThreadViewController.h"
#import "Messages.h" <--Need Messages Object to use Object and pass object around
#import "MessageManager.h"
@interface MessagesViewController () <UISearchBarDelegate, UISearchResultsUpdating, UIActionSheetDelegate, UISearchControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *inbox;
@property (strong, nonatomic) NSArray *sent;
@property (strong, nonatomic) NSArray *deleted;
@property (strong, nonatomic) NSArray *temp;
@property (assign, nonatomic) NSInteger selectedBox;
@property (assign, nonatomic) NSInteger selectedMessage;
@property (strong, nonatomic) MessageManager *messageManager;
@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
@property (strong, nonatomic) Messages *messages;
@end
MessageManager.h
#import <Foundation/Foundation.h>
@interface MessageManager : NSObject
- (void)getReceivedMessagesListForReceiverID:(NSString*)receiverID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)getSentMessagesListForSenderID:(NSString*)senderID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)getDeletedMessagesListForMemberID:(NSString*)memberID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchReceivedMessagesListForReceiverID:(NSString*)receuverID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchDeletedMessagesListForMemberID:(NSString*)memberID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)unreadMessagesCountForReceiverID:(NSString*)receiverID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)sendMessageToReceiverID:(NSString*)receieverID ToSenderID:(NSString*)senderID withTitle:(NSString*)title andMessage:(NSString*)message success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)markMessageRead:(NSString*)messageID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)markMessageUnRead:(NSString*)messageID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)receivedListCountForReceivedID:(NSString*)receivedID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)deletedMessagesListCountForMemberID:(NSString*)memberID success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchReceivedListCountForReceiverID:(NSString*)receiverID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchSentListCountForSender:(NSString*)senderID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
- (void)searchDeletedListCountForMemberID:(NSString*)memberID andKeyword:(NSString*)keyword success:(void (^)(NSArray *success))success failure:(void (^)(NSError *error))failure;
@end
MessageManager.m
#import "MessageManager.h"
#import "Constants.h"
#import <AFNetworking/AFNetworking.h>
#import "Messages.h" <--Need Messages Object to create Messages from response
@implementation MessageManager
...
错误
duplicate symbol _OBJC_IVAR_$_MessagesModel._result in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_MessagesModel._items in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_MessagesModel._numItems in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_CLASS_$_Messages in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_METACLASS_$_Messages in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_Messages._deleted_by_sender in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_MessagesModel._action in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_Messages._receiver_login in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
duplicate symbol _OBJC_IVAR_$_Messages._sender_login in:
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessagesViewController.o
/Users/anthonytaylor/Library/Developer/Xcode/DerivedData/Build/Intermediates/Voices.com.build/Debug-iphonesimulator/Voices.com.build/Objects-normal/x86_64/MessageManager.o
...
根据您在问题中提到的文件,您的项目中只有 Messages.h
文件而没有 Messages.m
文件,而是在 header 文件,我说的对吗?通过从您的问题中复制 classes,我能够在示例项目 I found here 上复制您的重复符号错误。但是当我创建一个 Messages.m 文件同时实现 Messages
和 MessageManager
classes 时,重复符号编译器错误消失了,我能够创建一个实例MessageManager
class.
话虽如此,我不确定您所说的
是什么意思The JSONModel tutorials all show doing the @implementation in the header, and not needing the .m files at all.
评论,但根据 JSONModel 的 Github readme page,他们说:
There's no need to do anything in the implementation (.m) file.
这可能并不意味着您不需要 .m 文件。无论如何,为 class 同时拥有 .h 和 .m 文件是一个好习惯,因为 Xcode 项目目标的 Build Phases
选项卡下的 Compile Sources
选项只有 .h 和 .m 文件。 m 个文件。这就是告诉编译器在构建目标时应该编译哪些源文件。由于您在 .h 文件中声明了实现,因此编译器在构建时无法找到它。
这是我设置的示例 Xcode 项目的屏幕截图:
MessageManager.h 文件
#import <Foundation/Foundation.h>
@class MessagesModel;
@interface MessageManager : NSObject
+ (MessagesModel *)getMessages;
@end
MessageManager.m 文件:
#import "MessageManager.h"
#import "Messages.h" //<-- Imported here
@implementation MessageManager
+ (MessagesModel *)getMessages {
NSError *error = nil;
NSDictionary *dictionary = @{@"action": @"replied",
@"dateTime": @"",
@"result": @"success",
@"numItems": @"1",
@"items": @[@{@"id": @"1",
@"sender_id": @"2",
@"receiver_id": @"3",
@"user_user_id": @"4",
@"job_id": @"5",
@"file_id": @"6",
@"title": @"Hello world",
@"replied" : @"7"}]};
MessagesModel *messages = [[MessagesModel alloc] initWithDictionary:dictionary error:&error];
if (error)
NSLog(@"Oops!");
return messages;
}
@end
Messages.h 文件:
#import <JSONModel/JSONModel.h>
@protocol Messages;
@interface Messages : JSONModel
@property (nonatomic) NSString<Optional>* id;
@property (nonatomic) NSString<Optional>* sender_id;
@property (nonatomic) NSString<Optional>* receiver_id;
@property (nonatomic) NSString<Optional>* user_user_id;
@property (nonatomic) NSString<Optional>* job_id;
@property (nonatomic) NSString<Optional>* file_id;
@property (nonatomic) NSString<Optional>* title;
@property (nonatomic) NSString<Optional>* replied;
@end
@protocol MessagesModel;
@interface MessagesModel : JSONModel
@property (nonatomic) NSString<Optional>* action;
@property (nonatomic) NSDate<Optional>* dateTime;
@property (nonatomic) NSString<Optional>* result;
@property (nonatomic) NSString<Optional>* numItems;
@property (nonatomic, strong) NSArray <Messages> *items;
@end
Messages.m 文件:
#import "Messages.h"
@implementation Messages
@end
@implementation MessagesModel
+ (JSONKeyMapper *)keyMapper {
return [JSONKeyMapper mapperForSnakeCase];
}
@end
ViewController.m 文件:
#import "ViewController.h"
#import "MessageManager.h"
#import "Messages.h" //<-- Imported here
@interface ViewController ()
@property (strong, nonatomic) MessagesModel *messagesModel;
@property (weak, nonatomic) NSArray<Messages *> *messages;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.messagesModel = [MessageManager getMessages];
self.messages = self.messagesModel.items;
NSLog(@"%@", self.messages);
}
@end
如果您希望我上传 Xcode 项目,请告诉我,以备您检查设置时使用。
希望这对您有所帮助。