Linker Command Failed with exit code 1: duplicate symbol - no .m file - ios

I've seen this issue in other posts but in my case I am using a sphere.h file that contains the data of vertices of a sphere, that is, a 3D model for my game project. I import my sphere.h file into my objective-C Class as follows:
#import "SceneEnergyGlobe.h"
#import "sphere.h"
#interface SceneEnergyGlobe() {
}
#property (strong, nonatomic) GLKTextureInfo *textureInfo0;
#end
.....
When I compile my project, I receive this compile error. How I can fix this?
Here is the contents of sphere.h:
#ifndef SPHERE_HEADER
#define SPHERE_HEADER
unsigned int sphereNumVerts = 2280;
float sphereVerts [] = {
0.0743889747124915, -0.49384436095363, -0.0241703260695731,
0.190555012144643, -0.979722062440628, -0.0619150039460291,
0.000000, 0.95,
0.0632787269334132, -0.49384436095363, -0.0459747512867777,
0.162096012330563, -0.979722074526971, -0.11776900895863,
0.050000, 0.95,
0.125000004921036, -0.475528075643002, -0.0908176095396332,
0.269869905435848, -0.942722669663907, -0.196071931295133,
.....

For every implementation file (.m) that includes sphere.h a new copy of the vertices array will be created. When these implementation files are linked together you get your duplicate symbol error.
The best approach is to change this in sphere.h:
#define sphereNumVerts 2280
extern float sphereVerts[sphereNumVerts];
And add an implementation file (sphere.m) that contains the definition of sphereVerts:
#import "sphere.h"
float sphereVerts[sphereNumVerts] = {
0.0743889747124915, -0.49384436095363, -0.0241703260695731,
...
};
BTW, those look like big numbers for float; are you sure you don't want double?
(EDIT: My previous suggestion was wrong; you'll get duplicate symbols using const in a header file too. I have changed this to a #define).

It is a very very very bad idea to create and initialize variables in .h files!!!

Related

In iOS how to use NS_ENUM?

I know that such questions are asked before. I looked on the net find examples but I can not figure out how to use NS_ENUM in my code.
I have a list of error codes. To manage them I want to use NS_ENUM. I create a class which contains all my public const for the application. I want to define my enum here and use it everywhere in the app.
My code in .h file:
typedef NS_ENUM(NSInteger, wsErrEnum);
In my .m file:
typedef NS_ENUM(NSInteger, wsErrEnum) {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 1
};
I thought that I can check in the following manner:
if(result.ErrCode == wsErrEnum.ERR_NONE) {
// do stuff ...
}
, but ERR_NONE is not visible as property.
So my question is how to define and use NS_ENUM in this case.
EDIT:
Using
if(result.ErrCode == ERR_NONE) {
NSLog(#"It is OK!");
}
give me error that ERR_NONE is undefined.
EDIT 2:
I understand the problem. I declare NS_ENUM in my .h file (as some comments and answers suggest) and the error disappears. I try this before but in that case I did not use enum properly - I use is as wsErrEnum.ERR_NONE. 10x for help.
P.S. My class is #imported in the .m file where I try to make this comparison.
The only problem here is that you've declared the enum's values in your .m file.
You should declare the entire enum (values included) in your .h file. By declaring the enum type wsErrNum in your .h file and the values in your .m file, you've hidden the values from all other classes.
So, in your .h file:
typedef NS_ENUM(NSInteger, wsErrEnum) {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 1
};
Nothing needs to go in your .m file to declare this enum.
In Swift :
if result.ErrCode == wsErrEnum.ERR_NONE {
// do stuff ...
}
In Objective-C :
if(result.ErrCode == ERR_NONE) {
// do stuff ...
}
The real issue is that you have split up the declaration of wsErrEnum, remove it from the .m file and place the full delcaration in the .h:
typedef NS_ENUM(NSInteger, wsErrEnum) {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 1
};
But better is something like:
typedef NS_ENUM(NSInteger, wsErrEnum) {
wsErrEnumName = 0,
wsErrEnumTimeOut = 1
};
Just a small remark, properties like your ErrCode should not started with a capital letter.
Normally, I use Enumerations in objective-c as typedef enum. Doing like this, you don't need an implementation file (.m) and the code looks like this:
// Enumerations.h
#interface Enumerations
typedef enum:int {
ERR_NONE,
ERR_HOST_TIMEOUT
} wsErrEnum;
#end
By default, the compiler values the enumerations from 0 to N, in order of which they are declared. In the example above, ERR_NONE is 0 and ERR_HOST_TIMEOUT is 1.
But you could also value the enumerations the way you preffer:
typedef enum:int {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 504
} wsErrEnum;
Then, you could work with switch and it would consider your enumeration, so it throws a warning in case you don't implement one possible result, for example:
switch(result.ErrCode) {
case ERR_NONE:
//implementation
break;
}
The code above will warn you that you don't have a case implemented for ERR_HOST_TIMEOUT.
You can also import your Enumerations.h in the file YourProjectName-Prefix.phc so it can be used in your hole project.
//
// Prefix header
//
// The contents of this file are implicitly included at the beginning of every source file.
//
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Enumerations.h" //Add this line so you don't have to import your .h file every time you use it
#endif

Swift Framework <-> ObjC interoperation

I've got a dylib framework with some UIView subclasses made in swift which I've done to use the new #IBDesignable and #IBInspectable stuff.
So lets say I've got a UITextField subclass in there named MyTextField.swift like this:
MyTextField.swift
#IBDesignable class MyTextField: UITextField {
// some properties etc.
// content is irrelevant
}
Now it compiles and works well in InterfaceBuilder so far so good. But what I need to do is import this special subclass into my Objective C implementation of the controller to set a property in code at runtime.
The framework (named myViews) has a header called myViews.h which I am able to import into the controllers header like this:
MyViewController.h:
#import myViews;
#import <UIKit/UIKit.h>
#import "myViews.h"
#interface MyViewController : UIViewController
#property(weak, nonatomic) IBOutlet MyTextField *txtName; // <-- This is the problem!
#end
This is where I'm stuck! So the class MyTextField is unknown from the imported headers. The myViews.h was automatically generated. I've tried to import the bridging-headers in there without success.
myViews.h:
#import <UIKit/UIKit.h>
//! Project version number for myViews.
FOUNDATION_EXPORT double myViewsVersionNumber;
//! Project version string for myViews.
FOUNDATION_EXPORT const unsigned char myViewsVersionString[];
// In this header, you should import all the public headers of your framework
// using statements like #import <myViews/PublicHeader.h>
#import <myViews/MyTextField-Swift.h> // <-- This is what I've tried to import here which doesn't work as well.
Hope anybody may help me out. Thanks in advance.
In your objective-c class, try importing your swift module like this:
#import "<ModuleName>-Swift.h"
Where ModuleName is the name of the module containing the swift code

How to get the correct autocomplete in XCode for a block variable?

I have a block thats stored as an instance variable in a class
typedef void ((^didSelectWord)(NSString* word));
#property (nonatomic,strong) didSelectWord wordSelected;
and i want xcode to auto fillout the block like when you type [UIView animateWithDuration and xcode autocompletes a block for it.
When i autocomplete my block it just fills out
[self.suggestedSearchTermView setWordSelected:(didSelectWord)wordSelected
instead of
[self.suggestedSearchTermView setWordSelected:^(NSString *word) {
Is it possible to change something to make Xcode understand how to autocomplete this block?
Ok I did some testing.
Apparently you have two (far from perfect) options:
avoid the typedef and declare the property as
#property (nonatomic,strong) void (^wordSelected)(NSString * word);
As noted in the comments, this has the drawback of skipping the parameter name in the autocompletion.
explicitly add a setter declaration in the interface
typedef void ((^DidSelectWordBlock)(NSString* word));
#interface YourClass : NSObject
#property (nonatomic,strong) DidSelectWordBlock wordSelected;
- (void)setWordSelected:(DidSelectWordBlock)wordSelected;
#end
this will cause Xcode to resolve the type definition before the setter definition, giving you the nice autocompletion that you would expect. The obvious drawback is the extra setter declaration in the interface.
That said, you should fill in a bug report: http://openradar.appspot.com/
Declare your property without typedef, like this:
#property (nonatomic,strong) void (^wordSelected)(NSString *word);
With this definition Xcode would give you the expansion below:
MyClass *test = [MyClass new];
[test setWordSelected:(void (^)(NSString *))wordSelected];
In exacerbated frustration, I made a macro consolidating this gross process..
#define BlockProperty(SIGNATURE,TYPENAME,varname,Varname) typedef SIGNATURE; #property (nonatomic,copy) TYPENAME varname; - (void) set##Varname:(TYPENAME)_
Now what Previously would've required (for proper autocompletion)..
typedef void(^OnEvent)(BOOL ok,id result);
#property (nonatomic,copy) OnEvent varname;
- (void) setVarname:(OnEvent)_;
is simply
BlockProperty(void(^OnEvent)(BOOL ok, id result),OnEvent,varname,VarName);
QUITE a bit easier, less verbose, AND you get the benefit of the typedef AND and you don't have to create the unsightly, theoretically unneeded setter declaration!
If you WANT to reuse a "type" you'll need another one (which this time will only take THREE parameters (as the block type cannot be redeclared).
#define BlockProp(TYPENAME,varname,Varname) #property (nonatomic,copy) TYPENAME varname; - (void) set##Varname:(TYPENAME)_
BlockProp(OnEvent,anotherVar,AnotherVar);
You could just create a new block type (name) for each property even if their signatures match (using the first macro), but that's kind of gross. Enjoy!

Why am I getting a type conflict warning on my enum in Objective-C?

Here is my Constants.h:
#import Foundation;
typedef NS_ENUM(NSUInteger, BarcodeType) {
kNormalBarcode,
kNoBarcode,
kGenericBarcode,
kInvalidBarcode,
kComicBarcode
};
#interface Constants : NSObject
#end
And here is a function that uses it:
.h:
#interface Helper : NSObject
- (BarcodeType)barcodeType:(NSString *)barcode;
.m:
#import "Constants.h"
...
- (BarcodeType)barcodeType:(NSString *)barcode
{
return kInvalidBarcode;
}
Why am I getting this warning on my function, and what do I need to change to fix it?
Conflicting return type in implementation of 'getBarcodeType:': 'id' vs 'BarcodeType' (aka 'enum BarcodeType')
This code seemed to work fine with older versions of Xcode.
Thanks!
Check your .h file. My guess is your declaration of the method uses id, which conflicts with your definition in the .m file that returns type BarcodeType. NSEnum values aren't objects, so id isn't valid. You'll want to correct the declaration so the return type matches the implementation.

Constants in Objective-C and "duplicate symbol" linker error

I've declared a constant with the same name in some different classes, in their .m file, this way:
#implementation MyViewController
const NSInteger numberOfItems = 6;
...
#end
But I get a "duplicate symbol" error when trying to build the project. I've found several posts dealing with this issue regarding extern or global constants, but what I'd want is just declaring some constants private to their class, how can I do that?
Thanks
If you want to use constant only in one .m file then declare it as static.
For example:static NSString * const CONSTANT_STRING = #"Constant I am".
In case of NSInteger you can write in your every .m file:
static const NSInteger my_const = 3;
If you want globals (one constant with one value visible in every file) then write in your .h:
extern const NSInteger my_global_const;
and in your .m file you can add
const NSInteger my_global_const = 5;

Resources