Where is a good place to place Category in Obj-c? - iphone

I've addeda category to NSString. I've tried to place the code in my appDelegate.h and it works. However, I've tried placing it in another .h file that I included from .pch file so that this category can be used project wide. However I kept getting the following error:
ld: duplicate symbol .objc_category_name_NSString_StringTrim in ....AppDelegate.o and .....main.o
collect2: ld returned 1 exit status
The weird thing is, I only get this error when I compile for the simulator but not device! Any ideas?

I always declare categories in Foo+Category.h and Foo+Category.m, for example NSString+Hashing.h and NSString+Hashing.m. In the source that uses the category I then import the Foo+Category.h and everything works wonders. The good thing about this solution is that the category gets mentioned explicitly in the code that uses it, so that the magic is apparent.

Related

1 duplicate symbol for architecture i386

I am facing a critical problem here, Xcode throws strange exception while building it's
"
duplicate symbol _selected in:
/Users/mhgaber/Library/Developer/Xcode/DerivedData/اProject-Name-aopcbghvorqhdwbyudzqsyhtekcu/Build/Intermediates/Project-Name.build/Debug-iphonesimulator/Project-Name.build/Objects-normal/i386/ClassX.o
/Users/mhgaber/Library/Developer/Xcode/DerivedData/Project-Name-aopcbghvorqhdwbyudzqsyhtekcu/Build/Intermediates/Project-Name.build/Debug-iphonesimulator/Project-Name.build/Objects-normal/i386/ClassY.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I searched a lot but I didn't find anything help me please
Look at both the files for ClassX and ClassY - What targets are they included in? Basically the _selected method is duplicated in both of them. I am going to guess this is a plain C method that happens to be named the same in both files. Try renaming _selected in one of the files.
In my case, I was declaring a const in a header file, which worked fine when building and running on the device (iPhone 5), however when attempting to simulate a 4S, all of a sudden I had some 300 "duplicate symbols".
It turns out I needed to also mark the const as static and the issue went away. Presumably it was trying to redefine the constant every time the header was referenced. The compiler isn't smart enough to just make constants static? Didn't think that would be necessary, but I guess it is.
const CGFloat kTitleAnimateDistance = 50.f;
Needed to be:
const static CGFloat kTitleAnimateDistance = 50.f;
Some time you accidentally importing the .m file instead of the .h due to which this error comes. Please check and If this is not the reason, then perform the following steps
1- Check Build phases in Target settings.
2- Go to compile source section.
3- Check if any file exists twice or once.
4- If file exist twice delete one.
5- Build again.
I was having the same problem and #dtrotzjr 's answer gave me a hint as to what could be causing it.
In my case I had a plain C void function in my framework (which xcode was complaining about as a duplicate symbol) and i needed to declare it as static void
I had the same issue. I was including a .h file with a number of const strings, methods and a struct. When I changed them all to static except the only mutable variable I wanted, it compiled just fine.

Duplicate Symbol Error for architecture i386

I got this error when i tried to build:
"duplicate symbol __Z8ERRCHECK11FMOD_RESULT in:
/Users/codemenmini2012-2/Library/Developer/Xcode/DerivedData/MagicSleepFullVersion-agxulkdijnxbqmbuigucmrczufyw/Build/Intermediates/MagicSleepFullVersion.build/Debug-iphonesimulator/MagicSleepFullVersion.build/Objects-normal/i386/MagicSleepViewController.o
/Users/codemenmini2012-2/Library/Developer/Xcode/DerivedData/MagicSleepFullVersion-agxulkdijnxbqmbuigucmrczufyw/Build/Intermediates/MagicSleepFullVersion.build/Debug-iphonesimulator/MagicSleepFullVersion.build/Objects-normal/i386/MagicSleepViewControllerIpad.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)"
How to solve this guys?
The error may occur when you copy and paste the contents of one file to another file with its interface name which means two classes with same interface name.
In your code you have two different files with the same Interface name.
For me this error happened because I was dumb enough to copy the whole folder of a downloaded lib to the project and there was a demo project inside it. So I had two main.m files. Hope this helps anyone!
In my case I had accidently imported .m file instead if .h file. Hope it helps someone for this kinda silly mistake.
when you create bool variables with same name in two different classes then this error comes.
"duplicate symbol __Z8ERRCHECK11FMOD_RESULT in"
so check your both classes
MagicSleepViewController.m and
MagicSleepViewControllerIpad.m.
for same bool variables.
Change the bool variable name, your problem will solve.
Looks like you have at least one (probably more) symbol (or methods, functions, etc.) that's duplicated between MagicSleepViewController.m and MagicSleepViewControllerIpad.m.
You need to either 1) change the names of one set of duplicated methods or 2) figure out a way to merge MagicSleepViewController.m & MagicSleepViewControllerIpad.m so the same code will work on both iPhones and iPads (e.g. using run time conditionals or whatever to determine what kind of device your code is currently running on).
I had #defines placed in two files that were exactly the same... DOH.
For me, a search in the finder for the named duplicates has helped.
The problem in my case was caused due to multiple references in the "Compile Sources". So I deleted one from Project->Build Phases-> Compile Sources.

Xcode: I just got an Apple Mach-O Linker (Id) Error and don't know why

I'm new to iOS Development, I'm using the latest version of Xcode and just got an error that said Apple Mach-O Linker (Id) Error exit code 1 and I haven't got a clue why. I think this is relevant but I'm not sure what it means:
ld: duplicate symbol _OBJC_CLASS_$_Timing1ViewController in /Users/tomkenning/Library/Developer/Xcode/DerivedData/EggTimer-ciznfdheqrtybuavrtbbcxfywyyw/Build/Intermediates/EggTimer.build/Debug-iphonesimulator/EggTimer.build/Objects-normal/i386/Mediumhb.o and /Users/tomkenning/Library/Developer/Xcode/DerivedData/EggTimer-ciznfdheqrtybuavrtbbcxfywyyw/Build/Intermediates/EggTimer.build/Debug-iphonesimulator/EggTimer.build/Objects-normal/i386/Timing1ViewController.o for architecture i386
All I've done recently is initialise and set some integer values in a .m file and then link to them from .h file from another ViewController, using #import "suchandsuch.m", there's been no errors in the code, but is that not allowed?
Thanks in advance for any help you can offer!
Don't do this:
#import "suchandsuch.m"
Do this:
#import "suchandsuch.h"
You are probably compiling suchandsuch.m, which defines the class Timing1ViewController, normally (by including suchandsuch.m in your target's list of files to build). Then your #import "suchandsuch.m" causes that same code to be inserted into a different source file, which is compiled as well. The result: two different source files try to define Timing1ViewController.
To do your constants the right way -- by declaring them extern in suchandsuch.h and defining them in suchandsuch.m -- see this answer.
You probably have two Timing1ViewController classes with the same name. If you don't try to Product -> Clean and build again.

iPhone app - copy and pasted a class, and now all goes wrong

I had a class called ReportsViewController in my iPhone app. I wanted to restore it form an older version of the class which I had copied to a text file. I copied and pasted the whole thing, and now everything is going wrong. I get the following error:
ld: duplicate symbol _OBJC_METACLASS_$_ReportsViewControllers in /Users/myname/Library/Developer/Xcode/DerivedData/TestGA-gipaszaocsrbkwashqjghthtsoda/Build/Intermediates/TestGA.build/Release-iphoneos/TestGA.build/Objects-normal/armv6/ReportsViewControllers.o and /Users/myname/Library/Developer/Xcode/DerivedData/TestGA-gipaszaocsrbkwashqjghthtsoda/Build/Intermediates/TestGA.build/Release-iphoneos/TestGA.build/Objects-normal/armv6/MasterGA.o for architecture armv6
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
How can I fix this?
Judging by the error, you've got two declarations of ReportsViewControllers. You need to find the one that you don't want and delete it.
Either there are two class-files or you "dubble-pasted" it, means there are two #implementaion MyController

$non_lazy_ptr link error with agvtool version variables?

I've been using agvtool for one of my iPhone apps on general principle, and have recently found a reason why I want to be able to check the version variable (so that I can re-copy help content into the Documents directory, if it's out of date). The variable, MyAppVersionNumber, defined in MyApp_vers.c, is auto-generated during the build. It gets generated in a .o file, and shows up in (the previous version?) of the linked app itself. So far so good, it would seem.
So now I've declared an extern double MyAppVersionNumber in the .m file where I need to use this (and later try to use it, just in an NSLog statement to get started), and I try to build for the iPhone simulator, and get a link error:
"_MyAppVersionNumber", referenced from:
_MyAppVersionNumber$non_lazy_ptr in HelpViewController.o
(maybe you meant: _MyAppVersionNumber$non_lazy_ptr)
ld: symbol(s) not found
collect2: ld returned 1 exit status
So, what do I need to do differently, to get this to link?
I googled for $non_lazy_ptr and agvtool, and got nothing at all, and for just $non_lazy_ptr, which gave some things, but mostly applying to libraries, which this isn't, really (is it?). And in my diggings so far, I've yet to turn up a page about agvtool that actually talks in any detail about using the variables -- the ones I've read just casually say to declare an extern variable, which I've done.
Anyone familiar with this, and/or otherwise able to provide any help?
Thanks!
Since agvtool updates your plist's CFBundleVersion value, I suggest accessing the version number at run-time with:
NSDictionary *mainDictionary = [[NSBundle mainBundle] infoDictionary];
double myVersionNumber = [(NSString *)[infoDictionary objectForKey:#"CFBundleVersion"] doubleValue];