Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"__weak typeof(self) weakSelf = self;" expected ; at end of declaration

Tags:

objective-c

I have two projects open in Xcode 6.4. Both use the follow declaration:

- (void)startService
{
    __weak typeof(self) weakSelf = self;
    [self.messageOperationQueue addOperationWithBlock:^{
        NSDictionary *storedMessages = [mySettings get:kStorageName withDefault:@{} storageType:kMySettingsStorageTypeDiskMapped];
        [weakSelf.messages addEntriesFromDictionary:storedMessages];
    }];
}

However, in one project there are no errors, and in the other project I get a compiler error:

Expected ';' at end of declaration

It wants me to put a semi-colon after __weak typeof(self). I have suspicion that I'm just doing something really dumb, but I'm having trouble figuring out what. Also, I know I could replace the typeof(self) with myClass *.

like image 813
Ben Flynn Avatar asked Aug 21 '15 16:08

Ben Flynn


2 Answers

Use __typeof instead of typeof.

This is because both __typeof and typeof are extensions to C, but typeof is only enabled in Clang when the language is a GNUXX variant of C, but not for CXX.

like image 165
Léo Natan Avatar answered Oct 15 '22 11:10

Léo Natan


I had a similar error. @Leo Natan is right and if you want, you can change the C dialect to GNU like this:

In the Project Navigator go to Project -> Target -> Build Settings

There look for C Language Dialect and change it from c11 or c99 to GNU99 and it will solve the problem. Now you can use typeof()

I hope it helps :)

like image 35
Ricardo Anjos Avatar answered Oct 15 '22 13:10

Ricardo Anjos