将自定义对象导入到viewController中时遇到了一些问题。
我创建了一个Message Object,它保存来自服务器的json响应。 然后我创建了一个MessageManager类,它执行对API的所有网络调用。
MessageManager导入Message,以创建消息对象并将其传递给正在使用它的任何类。
我现在有了我的viewController类,其中#import是MessageManager和Message。所以我可以创建一个Message的实例变量。 但是我在编译器中遇到了重复的符号错误。
如何解决此问题,并拥有正确的导入序列。
如果我从viewcontroller中删除Message.h,并删除任何引用Message的代码,它会正常编译。
#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
#import <UIKit/UIKit.h>
@interface MessagesViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UISearchController *searchController;
@end
#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
#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
#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
...
答案 0 :(得分:2)
根据您在问题中提到的文件,您的项目中只有Application.Resources
文件而没有Messages.h
文件,而是在标题中声明了实现档案,我是对的?通过复制问题中的类,我能够在示例项目found here上复制重复符号错误。但是当我创建一个带有Messages.m
和Messages
类实现的Messages.m文件时,重复符号编译器错误消失了,我能够创建MessageManager
类的实例
话虽如此,我不确定你的意思
JSONModel教程都显示在标题中执行@implementation,根本不需要.m文件。
评论,但根据JSONModel&#39; Github readme page,他们说:
无需在实现(.m)文件中执行任何操作。
这可能并不意味着您不需要拥有.m文件。无论如何,为一个类同时拥有.h和.m文件是一个很好的做法,因为Xcode项目的MessageManager
选项卡下的Compile Sources
选项只有.m文件文件。这是告诉编译器在构建目标时应编译哪些源文件的原因。由于您在.h文件中声明了实现,因此编译器无法在构建时找到它。
这是我设置的示例Xcode项目的屏幕截图:
MessageManager.h文件
Build Phases
MessageManager.m文件:
#import <Foundation/Foundation.h>
@class MessagesModel;
@interface MessageManager : NSObject
+ (MessagesModel *)getMessages;
@end
Messages.h文件:
#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.m文件:
#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
ViewController.m文件:
#import "Messages.h"
@implementation Messages
@end
@implementation MessagesModel
+ (JSONKeyMapper *)keyMapper {
return [JSONKeyMapper mapperForSnakeCase];
}
@end
如果您想要我检查设置,请告诉我您是否要上传Xcode项目。
希望这有帮助。