summaryrefslogtreecommitdiff
path: root/tests/examplefiles/objc_example.m
blob: f3f85f65063a21bd07822ff4572cd5e382208d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Test various types of includes
#import <Foundation/Foundation.h>
# import <AppKit/AppKit.h>
#import "stdio.h"
#\
  import \
  "stdlib.h"
# /*line1*/ \
import /* line 2 */ \
"stdlib.h" // line 3

// Commented out code with preprocessor
#if 0
#define MY_NUMBER 3
#endif

 #\
  if 1
#define TEST_NUMBER 3
#endif

// Empty preprocessor
#

// Class forward declaration
@class MyClass;

// Empty classes
@interface EmptyClass
@end
@interface EmptyClass2
{
}
@end
@interface EmptyClass3 : EmptyClass2
{
}
@end

// Custom class inheriting from built-in
@interface MyClass : NSObject
{
@public
  NSString *myString;
  __weak NSString *_weakString;
@protected
  NSTextField *_textField;
@private
  NSDate *privateDate;
}

// Various property aatributes
@property(copy, readwrite, nonatomic) NSString *myString;
@property(weak) NSString *weakString;
@property(retain, strong, atomic) IBOutlet NSTextField *textField;

// Class methods
+ (void)classMethod1:(NSString *)arg;
+ (void)classMethod2:(NSString *) arg; // Test space before arg

@end

typedef id B;

#pragma mark MyMarker

// MyClass.m
// Class extension to declare private property
@interface MyClass ()
@property(retain) NSDate *privateDate;
- (void)hiddenMethod;
@end

// Special category
@interface MyClass (Special)
@property(retain) NSDate *specialDate;
@end

@implementation MyClass
@synthesize myString;
@synthesize privateDate;

- (id)a:(B)b {
  /**
   * C-style comment
   */

  // Selector keywords/types
  SEL someMethod = @selector(hiddenMethod);

  // Boolean types
  Boolean b1 = FALSE;
  BOOL b2 = NO;
  bool b3 = true;

  /**
   * Number literals
   */
  // Int Literal
  NSNumber *n1 = @( 1 );
  // Method call
  NSNumber *n2 = @( [b length] );
  // Define variable
  NSNumber *n3 = @( TEST_NUMBER );
  // Arthimetic expression
  NSNumber *n4 = @(1 + 2);
  // From variable
  int myInt = 5;
  NSNumber *n5 = @(myInt);
  // Nest expression
  NSNumber *n6 = @(1 + (2 + 6.0));
  // Bool literal
  NSNumber *n7 = @NO;
  // Bool expression
  NSNumber *n8 = @(YES);
  // Character
  NSNumber *n9 = @'a';
  // int
  NSNumber *n10 = @123;
  // unsigned
  NSNumber *n11 = @1234U;
  // long
  NSNumber *n12 = @1234567890L;
  // float
  NSNumber *n13 = @3.14F;
  // double
  NSNumber *n14 = @3.14F;
  
  // Array literals
  NSArray *arr = @[ @"1", @"2" ];
  arr = @[ @[ @"1", @"2" ], [arr lastObject] ];
  [arr lastObject];
  [@[ @"1", @"2" ] lastObject];
  
  // Dictionary literals
  NSDictionary *d = @{ @"key": @"value" };
  [[d allKeys] lastObject];
  [[@{ @"key": @"value" } allKeys] lastObject];
  d = @{ @"key": @{ @"key": @"value" } };

  [self hiddenMethod];
  [b length];
  [privateDate class];

  NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"1", @"one", @"2", @"two", @"3", @"three", nil];
  
  NSString *key;
  for (key in dictionary) {
    NSLog(@"Number: %@, Word: %@", key, [dictionary valueForKey:key]);
  }

  // Blocks
  int (^myBlock)(int arg1, int arg2);
  NSString *(^myName)(NSString *) = ^(NSString *value) {
    return value;
  };

  return nil;
}

- (void)hiddenMethod {
  // Synchronized block
  @synchronized(self) {
    [myString retain];
    [myString release];
  }
}

+ (void)classMethod1:(NSString *)arg {}
+ (void)classMethod2:(NSString *) arg
{
  // Autorelease pool block
  @autoreleasepool {
    NSLog(@"Hello, World!");
  }
}

@end