1

I have a C function heightParameter that is a simple tool that I use in a couple of my UIViewControllers. I am declaring this only in my main implementation of each UIViewController subclass (in the .m) above my other functions, so that I didn't even have to declare it in the header.

For some reason, I'm getting duplicate symbols in every other subclass that I use it in, despite it being implemented privately. It is within the main @implementation @end block for each subclass and shouldn't be seen by anything else, so how is it being seen globally?

RileyE
  • 10,874
  • 13
  • 63
  • 106

1 Answers1

2

C function names have global scope. Mark it static or make it a method if you want it to be restricted.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Wouldn't static force it to be global? I thought static would make one implementation throughout the entire application? Or does it do that, but allow for multiple implementations? – RileyE Dec 28 '12 at 18:35
  • It's global *without* `static`. Adding `static` will restrict access to that symbol to a single compilation unit. That means you could have a separate implementation for each `.m` file. Why don't you just make it a private method, though? That would seem to be the most logical approach. A good reference for understanding `static`: http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program – Carl Norum Dec 28 '12 at 18:57
  • Oh. I guess I had a misunderstanding of what static was, then. Thanks! But by making it private, do you mean make it an Objective-C function? Or do you mean make it an Objective-C function AND add it to a private category? – RileyE Dec 28 '12 at 19:44
  • The reason that I didn't make it a private method because it didn't seem necessary and I didn't want to have to deal with the self and _cmd parameters. I find the less I need the app to do per call, the better. – RileyE Dec 28 '12 at 19:46
  • What's an Objective-C function? Functions in Objective-C are just C functions. I don't understand what you're saying about `self` or `_cmd`, either. Why do you think using a method will slow things down? – Carl Norum Dec 28 '12 at 20:12
  • Functions in objective-c send parameters `id self` and `SEL _cmd`, which I figured were unnecessary for this function. And the difference is in the parameters and how its written. One has a performance effect (as minimal as it is) and the other doesn't hold any merit. But, there are also some other differences, including the fact that they hold different connotations. If they were the same, then why can an objective-c function be private and not a c function? – RileyE Dec 28 '12 at 20:23
  • ... because Objective-C doesn't *have* anything like a function (except for C functions). It *only* has methods on classes or instances. – Carl Norum Dec 28 '12 at 21:22
  • Okay. I guess I never learned what the difference between a function and a method was, sadly. Now I know! – RileyE Dec 28 '12 at 22:53