0

In my header file, I declared

extern NSString * const UserDefault_ClusterItemWidth;

And in my implement file I gave it a value

NSString * const UserDefault_ClusterItemWidth = @"ClusterItemWidth";

Everything seems right, but when I called it

[[NSUserDefaults standardUserDefaults] setFloat:0.0f forKey:UserDefault_ClusterItemWidth];

I got

Thread 1: EXC_BAD_ACCESS (code=2, address=0x16fb1b700)

and XCode show something like this when I place the mouse on top of the constant

(NSString *const) UserDefault_ClusterItemWidth = 0x0000000100c1c698

Instead of @"ClusterItemWidth"

I am running out of idea on what is happening.

Thanks.

Kwok Ping Lau
  • 13
  • 1
  • 1
  • Most probably you have to use `@interface` and `@implementation` sections to declare and define Objective-C objects – Anton Malyshev Mar 06 '18 at 09:51
  • 1
    @AntonMalyshev Thanks for your comment. However, I believe that is not the case here, because the constant is a Global constant. From what I know, you declare the constant in `.h file` and give it a value in the `.m file`. No need of `@interface` or `@implementation`. Here is an example : [https://stackoverflow.com/questions/11932969/how-to-declare-and-use-nsstring-global-constants]. – Kwok Ping Lau Mar 06 '18 at 10:04
  • @KwokPingLau I think problem is from another place. Nothing happens when creating a new project with your above code. – trungduc Mar 06 '18 at 10:59
  • @trungduc thx, I will definitely take a closer look. But so far, no luck. – Kwok Ping Lau Mar 06 '18 at 12:24
  • @trungduc Thx again, yes, you are right. Apparently I accidentally created an infinite loop. – Kwok Ping Lau Mar 06 '18 at 12:45
  • Interesting, the following code seems so good, how does it crash? could you paste the crash stack info out? The two pointer addresses you mentioned are different, I believe that crash must happen in the other place. – Itachi Mar 08 '18 at 03:20
  • @Itachi Actually it doesn't show anything other than `Thread 1: EXC_BAD_ACCESS (code=2, address=0x16fb1b700)`. Also, yes, it occurred in other place. I call the NSUserDefaults infinite time by accident, so the app just crushed. – Kwok Ping Lau Mar 08 '18 at 04:49

1 Answers1

0

So apparently, I have accidentally created an infinite loop, setting the NSUserDefaults over and over again and causes the app to crush.

Kwok Ping Lau
  • 13
  • 1
  • 1