Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mac/Cocoa/QtCreator: problem using objective C "boxed expression"
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 4.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • crystalideaC Offline
    crystalideaC Offline
    crystalidea
    wrote on last edited by crystalidea
    #1

    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.

    1 Reply Last reply
    -1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • crystalideaC Offline
        crystalideaC Offline
        crystalidea
        wrote on last edited by crystalidea
        #3

        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
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • crystalideaC Offline
            crystalideaC Offline
            crystalidea
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • crystalideaC Offline
                crystalideaC Offline
                crystalidea
                wrote on last edited by crystalidea
                #7

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • crystalideaC Offline
                    crystalideaC Offline
                    crystalidea
                    wrote on last edited by crystalidea
                    #9

                    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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Are you linking against the Cocoa framework ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • crystalideaC Offline
                        crystalideaC Offline
                        crystalidea
                        wrote on last edited by crystalidea
                        #11

                        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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved