Understanding Xcode build errors from using Objective-C runtime function in OS X's Foundation app template.

  • On May 29, 2013, at 9:32 PM, <xcode-users-request...> wrote:
    > I'm learning how to use the Objective-C runtime function. To do this, I used Xcode Version 4.6.2 (4H1003) and chose from the OS X's Foundation template to create a new project with sample code I found in Apple's documentation.

    I didn't see your jpeg file (<XcodeScreenSnapz001.jpeg>) but when I put your code into an empty Xcode project I only got one build error and fixed it by typecasting malloc's returned value to (Class *).

    void listObjectiveC_ClassesUsedInThisApp(void)
    {
        int numClasses;
        Class * classes = NULL;

        classes = NULL;
        numClasses = objc_getClassList(NULL, 0);

        if (numClasses > 0 )
        {
            classes = (Class *) malloc(sizeof(Class) * numClasses);    // <<== added typecast to (Class *)
            numClasses = objc_getClassList(classes, numClasses);
            printf("This application has %u classes. Note that you cannot assume these classes inherit from NSObject.\n", numClasses);
            free(classes);
        }
    }

    Note: I also changed your source file's ("ObjectiveCRuntime.c") extension to ".m" (ObjectiveCRuntime.m); otherwise Xcode tried to compile the Foundation and ObjectiveCRuntime (ObjC) headers with C instead of ObjC; you can also fix this by selecting your source file in your project's "Project Navigator" view and then in the "File Inspector" view change your source files type to "Objective-C Source".

    --
    Enjoy,
    George Warner, x4-0668
    Schizophrenic Optimization Scientist
    Apple Developer Technical Support (DTS)
    eMail: <geowar...>
    iChat: <geodts...>
  • George, thanks for responding and helpful answer.

    I was dumbfounded before noticing my filename error. You're absolutely right and thanks for the tip on changing source file type.

    Have a great week!

    :)  Chris

    On May 31, 2013, at 1:38 AM, George Warner <geowar...> wrote:

    > On May 29, 2013, at 9:32 PM, <xcode-users-request...> wrote:
    >> I'm learning how to use the Objective-C runtime function. To do this, I used Xcode Version 4.6.2 (4H1003) and chose from the OS X's Foundation template to create a new project with sample code I found in Apple's documentation.
    >
    > I didn't see your jpeg file (<XcodeScreenSnapz001.jpeg>) but when I put your code into an empty Xcode project I only got one build error and fixed it by typecasting malloc's returned value to (Class *).
    >
    > void listObjectiveC_ClassesUsedInThisApp(void)
    > {
    > int numClasses;
    > Class * classes = NULL;
    >
    > classes = NULL;
    > numClasses = objc_getClassList(NULL, 0);
    >
    > if (numClasses > 0 )
    > {
    > classes = (Class *) malloc(sizeof(Class) * numClasses);    // <<== added typecast to (Class *)
    > numClasses = objc_getClassList(classes, numClasses);
    > printf("This application has %u classes. Note that you cannot assume these classes inherit from NSObject.\n", numClasses);
    > free(classes);
    > }
    > }
    >
    > Note: I also changed your source file's ("ObjectiveCRuntime.c") extension to ".m" (ObjectiveCRuntime.m); otherwise Xcode tried to compile the Foundation and ObjectiveCRuntime (ObjC) headers with C instead of ObjC; you can also fix this by selecting your source file in your project's "Project Navigator" view and then in the "File Inspector" view change your source files type to "Objective-C Source".
    >
    >
    > --
    > Enjoy,
    > George Warner, x4-0668
    > Schizophrenic Optimization Scientist
    > Apple Developer Technical Support (DTS)
    > eMail: <geowar...>
    > iChat: <geodts...>
    >
previous month may 2013 next month
MTWTFSS
    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    
Go to today