iOS - 链接器错误,重复的符号

7

当我尝试编译时,出现了一个错误,似乎直接指向两个文件:MenuViewController和FirstTopViewController。我认为问题出在我的导入上,因为它们每个都在导入另一个,但是错误引用了我的Building对象,这个对象在里面几乎没有用到。另外,对于clang错误,我该如何使用-v查看调用?

错误信息:

ld: duplicate symbol _OBJC_CLASS_$_Building in /Users/alexmuller/Library/Developer/Xcode/DerivedData/ECSlidingViewController-gjxwxiwumgohyehiawnlamggzmop/Build/Intermediates/ECSlidingViewController.build/Debug-iphonesimulator/ECSlidingViewController.build/Objects-normal/i386/FirstTopViewController.o and /Users/alexmuller/Library/Developer/Xcode/DerivedData/ECSlidingViewController-gjxwxiwumgohyehiawnlamggzmop/Build/Intermediates/ECSlidingViewController.build/Debug-iphonesimulator/ECSlidingViewController.build/Objects-normal/i386/MenuViewController.o for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

FirstTopViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#import "TimesViewController.h"
#import "BuildingViewController.h"
#import "BuildingAnnotation.h"
#import <MapKit/MapKit.h>

@class BuildingViewController;

@interface FirstTopViewController : UIViewController <MKMapViewDelegate> {
    IBOutlet MKMapView *_map;
    BuildingViewController *buildingVC;
    BuildingAnnotation *buildAnnotation;
}

@property (nonatomic, strong) MKMapView *map;

- (IBAction)revealMenu:(id)sender;
- (IBAction)revealTimes:(id)sender;
- (void)loadBuilding:(Building *)building;

@end

FirstTopViewController.m

#import "FirstTopViewController.h"

@implementation FirstTopViewController

@synthesize map = _map;

- (void)viewDidLoad {

    buildingVC = (BuildingViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"BuildingList"];
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [(UIView *)[self.view viewWithTag:10] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"RedWithNoise"]]];
    [super viewWillAppear:animated];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"toolbar"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:102.0/255.0 alpha:1.0], 
      UITextAttributeTextColor, 
      [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
      UITextAttributeTextShadowColor, 
      [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
      UITextAttributeTextShadowOffset, 
      [UIFont fontWithName:@"AGaramondPro-Regular" size:23.0], 
      UITextAttributeFont, 
      nil]];
    self.view.layer.shadowOffset = CGSizeZero;
    self.view.layer.shadowOpacity = 0.75f;
    self.view.layer.shadowRadius = 10.0f;
    self.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.layer.bounds].CGPath;
    self.view.clipsToBounds = NO;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(30.451667, -84.268533), 16090.344, 16090.344);
    viewRegion = [_map regionThatFits:viewRegion];
    [_map setRegion:viewRegion animated:YES];

    if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
        self.slidingViewController.underLeftViewController  = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
    }

    if (![self.slidingViewController.underRightViewController isKindOfClass:[TimesViewController class]]) {
        self.slidingViewController.underRightViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Times"];
    }

    [self.view addGestureRecognizer:self.slidingViewController.panGesture];


}

- (void)loadBuilding:(Building *)building {
    if (buildingVC.buildingSelected) {
        if (buildAnnotation != nil) {
            [_map removeAnnotation:buildAnnotation];
        }
        NSLog(@"%@", building.getName);
        buildAnnotation = [[BuildingAnnotation alloc] initWithCoordinate:building.getLocation.coordinate withName:building.getName withAddress:building.getAddress];
        [_map setCenterCoordinate:buildAnnotation.coordinate animated:YES];
        [_map addAnnotation:buildAnnotation];
    }
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  self.view.layer.shadowPath = nil;
  self.view.layer.shouldRasterize = YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
  self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.layer.bounds].CGPath;
  self.view.layer.shouldRasterize = NO;
}

- (IBAction)revealMenu:(id)sender
{
  [self.slidingViewController anchorTopViewTo:ECRight];
}

- (IBAction)revealTimes:(id)sender {
    [self.slidingViewController anchorTopViewTo:ECLeft];
}

@end

MenuViewController.h

#import <UIKit/UIKit.h>
#import "ECSlidingViewController.h"
#import "FirstTopViewController.h"
#import "TimesViewController.h"

@interface MenuViewController : UIViewController <UITableViewDataSource, UITabBarControllerDelegate> {
    NSIndexPath *selectedIndex;
}

@end

MenuViewController.m

#import "MenuViewController.h"

typedef enum {
    ENGINEERING,
    GARNET,
    GOLD,
    HERITAGE,
    NIGHT,
    OSCEOLA,
    RENEGADE,
    TOMAHAWK
} RouteName;


@interface MenuViewController()
@property (nonatomic, strong) NSArray *menuItems;
@property (nonatomic, strong) NSArray *optionItems;
@property (nonatomic, strong) NSArray *arrayItems;

- (UIImage *)determineActiveRoute:(RouteName)route;
@end

@implementation MenuViewController
@synthesize menuItems, optionItems, arrayItems;

- (void)awakeFromNib
{

    self.menuItems = [NSArray arrayWithObjects:@"Engineering", @"Garnet", @"Gold", @"Heritage Grove", @"Night Nole", @"Osceola", @"Renegade", @"Tomahawk", nil];
    self.optionItems = [NSArray arrayWithObjects:@"Buildings", @"Directions", nil];
    self.arrayItems = [NSArray arrayWithObjects:self.menuItems, self.optionItems, nil];
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self.slidingViewController setAnchorRightRevealAmount:200.0f];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
  return [[self.arrayItems objectAtIndex:sectionIndex] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.arrayItems count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] init];
    [sectionView sizeToFit];
    sectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navCellDivider"]];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20)];
    label.textColor = [UIColor grayColor];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"AGaramondPro-Regular" size:12.0];
    if(section == 0)
        label.text = @"BUS ROUTES";
    else
        label.text = @"OTHER OPTIONS";
    [sectionView addSubview:label];
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *label;
    UIImageView *imageView;
    NSString *cellIdentifier = @"MenuItemCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    }
    UIView *selectedView = [[UIView alloc] initWithFrame:cell.frame];
    selectedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navCellBackgroundSelected"]];
    cell.selectedBackgroundView = selectedView;
    cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navCellBackground"]];
    label = (UILabel *)[cell.contentView viewWithTag:100];
    label.font = [UIFont fontWithName:@"AGaramondPro-Regular" size:15.0];
    label.text = [[self.arrayItems objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    imageView = (UIImageView *)[cell.contentView viewWithTag:101];
    if (indexPath.section == 0) {
        [imageView setImage:[self determineActiveRoute:indexPath.row]];
    } else {
        if (indexPath.row == 0) {
            [imageView setImage:[UIImage imageNamed:@"building"]];
        } else {
            [imageView setImage:[UIImage imageNamed:@"60-signpost"]];
        }
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* Type cast view as a First Top to make it easier to read. Do switch to select correct route,
        then modify map afterwards to load up correct route.
     TimesViewController can be set to the corresponding times when selected
     */
    FirstTopViewController *mapViewController = (FirstTopViewController *)self.slidingViewController.topViewController;
    TimesViewController *timesViewController = (TimesViewController *)self.slidingViewController.underRightViewController;

    if (indexPath.section == 0) {
        selectedIndex = indexPath;
        switch (indexPath.row) {
            case ENGINEERING:
                NSLog(@"Engineering Selected\n");
                timesViewController.times.text = @"Engineering";
                break;
            case GARNET:
                NSLog(@"Garnet Selected\n");
                timesViewController.times.text = @"Garnet";
                break;
            case GOLD:
                NSLog(@"Gold Selected\n");
                timesViewController.times.text = @"Gold";
                break;
            case HERITAGE:
                NSLog(@"Heritage Grove Selected\n");
                timesViewController.times.text = @"Heritage Grove";
                break;
            case NIGHT:
                NSLog(@"Night Nole Selected\n");
                timesViewController.times.text = @"Night Nole";
                break;
            case OSCEOLA:
                NSLog(@"Osceola Selected\n");
                timesViewController.times.text = @"Osceola";
                break;
            case RENEGADE:
                NSLog(@"Renegade Selected\n");
                timesViewController.times.text = @"Renegade";
                break;
            case TOMAHAWK:
                NSLog(@"Tomahawk Selected\n");
                timesViewController.times.text = @"Tomahawk";
                break;
            default:
                break;
        }
    } else {
        if (indexPath.row == 0) {
            BuildingViewController *buildings = [self.storyboard instantiateViewControllerWithIdentifier:@"BuildingList"];
            buildings.userLoc = [[CLLocation alloc] initWithLatitude:mapViewController.map.userLocation.coordinate.latitude longitude:mapViewController.map.userLocation.coordinate.longitude];

            [self presentModalViewController:buildings animated:YES];
        }
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        [tableView selectRowAtIndexPath:selectedIndex animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    [self.slidingViewController anchorTopViewTo:ECRight animations:nil onComplete:^{
        [self.slidingViewController resetTopView];
    }];
}

- (UIImage *)determineActiveRoute:(RouteName)route {
    NSCalendar *gregorianCalender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalender components:NSHourCalendarUnit fromDate:[NSDate date]];
    switch (route) {
        case ENGINEERING:
            if ([components day] % 7 == 2) {
                if (([components hour] >= 7) && ([components hour] < 17)) {
                    return [UIImage imageNamed:@"green"];
                } else {
                    return [UIImage imageNamed:@"red"];
                }
            } else if (([components day] % 7 == 3) || ([components day] % 7 == 4)) {
                return [UIImage imageNamed:@"red"];
            } else {
                //Weekday
                if (([components hour] >= 7) && ([components hour] < 19)) {
                    return [UIImage imageNamed:@"green"];
                } else {
                    return [UIImage imageNamed:@"red"];
                }
            }
            break;
        case GARNET:
        case GOLD:
        case HERITAGE:
            if ((([components day] % 7 <= 3) || ([components day] % 7 >= 5)) &&
                (([components hour] >= 7) && ([components hour] <= 19))) {
                return [UIImage imageNamed:@"green"];
            } else {
                return [UIImage imageNamed:@"red"];
            }
            break;
        case NIGHT:
            if ((([components day] % 7 == 6) || ([components day] % 7 <= 3)) &&
                (([components hour] >= 22.5) || ([components hour] <= 3))) {
                return [UIImage imageNamed:@"green"];
            } else {
                return [UIImage imageNamed:@"red"];
            }
            break;
        case OSCEOLA:
        case RENEGADE:
        case TOMAHAWK:
            if ((([components day] % 7 <= 3) || ([components day] % 7 >= 5)) &&
                (([components hour] >= 7) && ([components hour] <= 19))) {
                return [UIImage imageNamed:@"green"];
            } else {
                return [UIImage imageNamed:@"red"];
            }
            break;
    }
}

@end

1
重复符号意味着你的项目或编译列表中有两个命名为“Builder”的类。 - CodaFi
1
我有Building.h、BuildingAnnotation.h和BuildingViewController.m/h文件。有没有一种方法可以清除编译列表并让Xcode重新生成它? - Alex Muller
1
清空编译列表,然后当您要重新添加所有内容时,请搜索术语“.m”(不带引号),并突出显示每个.m文件。然后单击添加。 - CodaFi
1
这个“编译列表”在哪里?你是指构建阶段下的编译源吗? - Alex Muller
1
@Muller 这正是我所指的。至于 Thomas 的评论,我会先回应它。 - CodaFi
显示剩余3条评论
7个回答

26

检查您是否导入了.m文件,而应该是.h文件。Doh!


太棒了!我都快疯了,从来没有仔细地寻找过这个。 - IMFletcher

18

根据您的请求:

从编译源中移除所有类,然后点击“+”按钮并搜索术语“.m”。 高亮显示每个类,然后点击添加。 再次构建和运行。


请检查您的 @implementation 行是否具有正确的名称。 - Bradley Thomas
2
我能够进入编译源代码,找到报告重复符号的特定文件。将其删除并重新添加,然后它就可以工作了。本质上与这个答案相同,但没有炸掉整个东西。 - teradyl

2

当我将多个*.a文件的第三方库导入我的IOS项目时,遇到了类似的错误。 在我的情况下,删除“-all_load”链接器标志解决了这个问题。


这样做消除了大多数重复项。仍剩下4个。 - John Franke

1
因为 ICMGameDemoCMGameSDK 都将.m文件添加到了compile sources面板中,所以我遇到了这个问题。我删除了ICMGameDemo并重新构建,问题就解决了。

project settings screenshot


1
删除文件并选择“删除引用”。将文件再次拖回项目中,应该就可以了。这对我起作用了。

1
我遇到的问题是我复制了一个m文件,但没有重命名@implementation。确保您没有重复的@ implementation声明。

1

不确定,但可能有几件事情。

  1. 我注意到两个类似乎都没有导入Building.h文件,所以我的第一个想法是编译器不知道从哪里获取这个类的信息。您需要添加 #import 吗?

  2. 下一步是我通常尝试避免两个相互导入的类。编译器可能会在尝试确定编译顺序时出现问题。您可能需要更改其中一个以使用@class ...而不是#import...,以便编译器在运行时解析类。

  3. 最后,您是否进行了清理?有时在大量重构时,旧的类定义会留在构建目录中,可能会引起此类问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接