Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is really really basic beginner stuff that suggests to me you don't understand the difference between C-family language's "header files" and "implementation files".</p> <p>Did you search your project and not notice that you are declaring variables in header files but without <code>extern</code>? You even had the name of the global variable in your linker error. Yes, global variables are emitted as symbols into object files (.o) and if you include a header into three files and all three files then declare the same global variable, you get this error. It was for just this issue that the mighty and wonderful <code>extern</code> keyword was invented. </p> <p>In the context of header files you should not declare actual variables, only <code>extern</code> references to them. This is something you should learn on your first day of C, C++ or Objective C programming. You can however put code into header files, but then, every file you include that header into will generate a duplicate set of symbols, leading to link errors, and further accelerating the pace of global warming, and causing the melting of icebergs. (Just kidding about that last part.)</p> <p>Here is your mistake, this is a header file but you are treating it like it was an implementation file and declaring not external-variable-declarations but actual-emit-the-global-variable-into-the-object-file declarations:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; UIImage *SavedImage; UIImage *SavedImage1; NSString * vvvalue; //UILabel * l2; NSString * lll; @interface NSObject (ShareClass) @end </code></pre> <p>You tell me now. Where should the keyword <code>extern</code> appear in the code sample above? Where does the real "emit this variable into object file" declaration of a variable go? In a header file? NO. In the implementation file (.m or .c or .cpp as the case may be). </p> <p>Secondly, after you add the extern keyword, you now have a second problem. You probably need to add the real declaration of that global variable in your corresponding <code>.m</code> file. Yes, that's right you have to "declare" every global twice in a C-family language program. once as extern in header files if you really need all the modules in your system to be able to <em>see that global variable</em>, and once without the extern keyword, in only one of your .m files.</p> <p>Of course, you do know that such "global variable mess" is to be avoided, and is terrible programming practice right? You should be thinking about keeping global variables out of your code, and have variables that limit their scope to a single implementation module wherever possible.</p> <p>In .h file:</p> <pre><code>extern UIImage * myImage; </code></pre> <p>In .m file:</p> <pre><code>UIImage *myImage; </code></pre> <p>Go read your book on what the <code>extern</code> keyword means please. Also, I suspect that you might have meant to do this:</p> <pre><code>@interface NSObject (ShareClass) UIImage * InstanceVariable; @end </code></pre> <p>The above is an instance variable (belongs to each instance of the class). It is common if you need to group some stuff and put it inside an object not to make it a global variable.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload