Domanda

I just started learning "accessor methods" on Objective C and I get stuck. I couldn't find why I am getting that "Duplicate Symbol Error" that stops the program from running. Should be a easy thing! but I will appreciate some help.

The xCode issue navigator complains:

Ld /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug/Shapes-Object normal x86_64
cd "/Users/leo/Google Drive/Documentos/Formación/Curso iOS/Pre iOS, pilares/Learn Objective C on the Mac/Ejercicios/Chapter 06/SPLIT 04.02 Shapes-Green-Circles MAS FACIL"
export MACOSX_DEPLOYMENT_TARGET=10.7
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -                     L/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug -F/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug -filelist /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Shapes-Object.LinkFileList -mmacosx-version-min=10.7 -fobjc-link-runtime -framework Foundation -Xlinker -dependency_info -Xlinker /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Shapes-Object_dependency_info.dat -o /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug/Shapes-Object

duplicate symbol _colorPrint in:
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/main.o
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Circulo.o
duplicate symbol _colorPrint in:
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/main.o
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/SALCHICHA.o
duplicate symbol _colorPrint in:
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/main.o
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Shape.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

That is my main():

#import <Foundation/Foundation.h>

#import "Circulo.h"
#import "SALCHICHA.h"



void FuncionDibujarFormas (id Formas[], int count)
{
for (int i = 0; i < count; i++) {
    id shape = Formas[i];
    [shape Dibujar];
}

} // FuncionDibujarFormas





// --------------------------------------------------
// The main function.  Make the shapes and draw them

int main (int argc, const char * argv[]) 
{
id Formas[3];


Formas[0] = [Circulo new];
[Formas[0] definirColor: kRojo];

Formas[2] = [SALCHICHA new];
[Formas[2] definirColor: kVerde];




FuncionDibujarFormas (Formas, 3);

return (0);

} // main

That is the "superclass" header for Shape.h:

#import <Foundation/Foundation.h>

typedef enum {

kRojo,
kVerde,

} Color;


//--------------------------------------------------

NSString *colorPrint (Color switchColor) //"Color" se asigna "switchColor"
{
switch (switchColor) {
    case kRojo:
        return @"rojo";
        break;
    case kVerde:
        return @"verde";
        break;

}

return @"malign@";

 } // colorPrint

 @interface Shape : NSObject
 {
Color   RellenarColor;

 }

 - (void) definirColor: (Color) RellenarColor;

 - (void) Dibujar;


 @end // Shape

That's the Shape.m:

#import "Shape.h"

@implementation Shape

- (void) definirColor: (Color) ImplementationRellenarColor  
{

RellenarColor = ImplementationRellenarColor;

} // definirColor

- (void) Dibujar
{
} // draw

@end // Shape

That's the "childclass" Circulo.h:

#import "Shape.h"

@interface Circulo : Shape
@end // Circulo  

Thats the child class Circulo.m :

#import "Circulo.h"

@implementation Circulo

- (void) definirColor: (Color) ImplementationRellenarColor
{
if ( ImplementationRellenarColor == kRojo ) { 

    ImplementationRellenarColor = kVerde;
}
[super definirColor: ImplementationRellenarColor];

} // definirColor



- (void) dibujar
{
NSLog (@"dibujando un circulo %@", colorPrint(RellenarColor)); 
} // dibujar

@end // Circulo

That's "chilclass" SALCHICHA.h:

#import "Shape.h"


@interface SALCHICHA : Shape

@end // SALCHICHA

Thats chilclass SALCHICHA.m

#import "SALCHICHA.h"

@implementation SALCHICHA

- (void) dibujar
{
NSLog (@"dibujando una SALCHICHA %@", colorPrint(RellenarColor)); 

} // dibujar


@end // SALCHICHA
È stato utile?

Soluzione

You have a C function implementation in a header file (Shape.h): NSString *colorPrint (Color switchColor) //"Color" se asigna "switchColor" Each time you import the header you duplicate the symbol.

So using in pure Objc you should have someting like:

In header:

+(NSString*)colorPrint:(Color)switchColor;

In implementation:

+(NSString*)colorPrint:(Color)switchColor {
    switch (switchColor) {
        case kRojo:
            return @"rojo";
        case kVerde:
            return @"verde";
    }
    return @"malign@";
}

And to call it:

NSLog (@"dibujando un circulo %@", [Shape colorPrint:RellenarColor]);

Edit: Fixed parameter name

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top