Mac/Cocoa/QtCreator: problem using objective C "boxed expression"



  • Hello!

    I have Qt Creator 3.6 + XCode 5. Accessing array object using brackets syntax doesn't work! (though it should work starting from Xcode 4.5)

    Example:

    #if __has_feature(objc_boxed_expressions)
    NSArray *array = @[@"value1", @"value2"];
    id object = array[1];
    #endif
    

    Result: error: initializing 'id' with an expression of incompatible type 'NSArray'
    id object = array[1];
    ^ ~~~~~~~~

    Anyone has a solution? Thank you.


  • Lifetime Qt Champion

    Hi,

    Where did you put that code ? How did you integrate it in your project ?



  • Hi, sgaist!

    The .mm file is added into the .pro file via macx { OBJECTIVE_SOURCES += ...}
    Trying to compile piece of code from Sparkle:

    #import "SUOperatingSystem.h"
    
    @implementation SUOperatingSystem
    
    + (NSOperatingSystemVersion)operatingSystemVersion
    {
    #if __MAC_OS_X_VERSION_MIN_REQUIRED < 101000
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wselector"
        if (![NSProcessInfo instancesRespondToSelector:@selector(operatingSystemVersion)])
    #pragma clang diagnostic pop
        {
            NSOperatingSystemVersion version = { 0, 0, 0 };
            NSURL *coreServices = [[NSFileManager defaultManager] URLForDirectory:NSCoreServiceDirectory inDomain:NSSystemDomainMask appropriateForURL:nil create:NO error:nil];
            NSArray *components = [[NSDictionary dictionaryWithContentsOfURL:[coreServices URLByAppendingPathComponent:@"SystemVersion.plist"]][@"ProductVersion"] componentsSeparatedByString:@"."]; // **PROBLEM!**
            version.majorVersion = components.count > 0 ? [components[0] integerValue] : 0; // **PROBLEM!**
            version.minorVersion = components.count > 1 ? [components[1] integerValue] : 0; // **PROBLEM!**
            version.patchVersion = components.count > 2 ? [components[2] integerValue] : 0; // **PROBLEM!**
            return version;
        }
    #endif
        return [[NSProcessInfo processInfo] operatingSystemVersion];
    }
    
    @end
    

  • Lifetime Qt Champion

    Are you looking for the information you can get from QSysInfo ?



  • I'd really love to know why I can't write such a simple code:

    #if __has_feature(objc_boxed_expressions)
    NSArray *array = @[@"value1", @"value2"];
    id object = array[1];
    #endif
    

    Because I'm going to compile more .mm files in the project. It's strange and now a matter of honor to discover what's going on.


  • Lifetime Qt Champion

    Tested that one in a simple main.mm file, no problem except the warning about unused object variable.



  • Sure it's a valid code. Something is wrong with compiler/IDE setup I guess.


  • Lifetime Qt Champion

    Did you try to build a minimal dummy project with it ?



  • Yes, created the following test.mm file

    void f()
    {
        #if __has_feature(objc_boxed_expressions)
        NSArray *array = @[@"value1", @"value2"];
        id object = array[1];
        #endif
    }
    
    22:59:40: Running steps for project untitled...
    22:59:40: Configuration unchanged, skipping qmake step.
    22:59:40: Starting: "/usr/bin/make" 
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -c -pipe -arch i386 -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.7 -Wall -W -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../untitled -I. -I/usr/local/Qt-5.5.1/lib/QtWidgets.framework/Headers -I/usr/local/Qt-5.5.1/lib/QtGui.framework/Headers -I/usr/local/Qt-5.5.1/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I/usr/local/Qt-5.5.1/mkspecs/macx-clang-32 -F/usr/local/Qt-5.5.1/lib ../untitled/test.mm -o test.o
    ../untitled/test.mm:4:5: error: unknown type name 'NSArray'
        NSArray *array = @[@"value1", @"value2"];
        ^
    ../untitled/test.mm:4:22: error: NSArray must be available to use Objective-C array literals
        NSArray *array = @[@"value1", @"value2"];
                         ^
    2 errors generated.
    make: *** [test.o] Error 1
    22:59:40: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project untitled (kit: Desktop)
    When executing step "Make"
    22:59:40: Elapsed time: 00:00.
    

    Clang version:

    Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
    Target: x86_64-apple-darwin15.3.0
    Thread model: posix


  • Lifetime Qt Champion

    Are you linking against the Cocoa framework ?



  • Not sure if it's a problem of linker. (compiler?) here's the .pro file, added the framework line, no success

    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-02-01T22:56:54
    #
    #-------------------------------------------------
    
    QT       += core gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = untitled
    TEMPLATE = app
    SOURCES += main.cpp\
            mainwindow.cpp
    HEADERS  += mainwindow.h
    FORMS    += mainwindow.ui
    OBJECTIVE_SOURCES += \
        test.mm
    
    LIBS += -framework Cocoa
    

    PS.
    Also added the line
    #import <Foundation/Foundation.h>

    To remove "NSArray must be available to use Objective-C array literals" error


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.