Skip to content

Instantly share code, notes, and snippets.

@vitonzhangtt
Last active January 5, 2018 04:46
Show Gist options
  • Save vitonzhangtt/1fa7ade1280ce8e598ec404d9a9aa986 to your computer and use it in GitHub Desktop.
Save vitonzhangtt/1fa7ade1280ce8e598ec404d9a9aa986 to your computer and use it in GitHub Desktop.
@synchronized keyword
@class TTUserObject;
@interface TTFriendsCollection : NSObject
- (void)addFriend:(TTUserObject *)user;
- (void)removeFriend:(TTUserObject *)user;
@end
#import "TTFriendsCollection.h"
#import "TTUserObject.h"
@interface TTFriendsCollection ()
@property(nonatomic, strong) NSMutableArray *friendsArray;
@end
@implementation TTFriendsCollection
- (void)addFriend:(TTUserObject *)user {
@synchronized (self.friendsArray) {
if (!self.friendsArray) {
self.friendsArray = [NSMutableArray array];
}
[self.friendsArray addObject:user];
}
}
- (void)removeFriend:(TTUserObject *)user {
@synchronized (self.friendsArray) {
if (self.friendsArray) {
[self.friendsArray removeObject:user];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment