code generation objective-c

When creating the data tables it’s common to have a number of relationships between each table. When generating code this results in references between the generated classes. In languages like Objective-C where the interface is separate from the implementation you can’t have 2 files where each includes the other. i.e. User.h can’t include Business.h if Business.h also includes User.h

For the Objective-C case the .h files should use @class and the import should go in the .m file. Currently this modification needs to be made manually.

Additionally, for swift compatibility, relationship properties should be defined with additional type information, such as:

@property (strong, nonatomic) NSArray<User *> * users; 

Though this does limit the application to more current version of the Xcode toolchain (though that shouldn’t be an issue in practice).

Hi Wain,

Could you provide your code generated project with your data model classes to support@backendless.com - we investigate these problems and will fix it in our next server-side release.

Thank you,
Slava

Hi Slava,

This isn’t a problem as such, it’s just some recommendations about how the Objective-C code generation can be improved. In order to get the code to compile initially I needed to make the @class changes and then to use with Swift effectively I made the arrays strongly typed.

For example, a code generated file started as:

#import <Foundation/Foundation.h>


#import "Place.h"


@interface Business : NSObject


@property (strong, nonatomic) Place * location;
@property (strong, nonatomic) NSArray * members;




@end

and I edited it to:









#import <Foundation/Foundation.h>




@class Place;


@class Membership;




@interface Business : NSObject


@property (strong, nonatomic) Place * location;

@property (strong, nonatomic) NSArray<Membership *> * members;




@end

I just created the Data/CRUD project with codegen.
My data model classes have 1:1 and 1:N relations:

class Address : NSObject {
 
 var street : NSString?
 var city: NSString?
 var state : NSString?
}

class Contact : NSObject {
 
 var objectId : String?
 var name : String?
 var age : Int = 0
 var phone : String?
 var title : String?
 var homeAddress : Address?
 var weight : Double = 0.0
}

class PhoneBook : NSObject {
 
 var objectId : String?
 var owner : Contact?
 var contacts : [Contact] = []
 var status : Int = 0
}


In code generated project these classes are:

  • Address.h:
#import <Foundation/Foundation.h>

@interface Address : NSObject
@property (nonatomic, strong) NSDate *created;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *ownerId;
@property (nonatomic, strong) NSString *street;
@property (nonatomic, strong) NSDate *updated;
@property (nonatomic, strong) NSString *objectId;
@end
  • Contact.h
@class Address;

@interface Contact : NSObject
@property (nonatomic, strong) NSString *ownerId;
@property (nonatomic, strong) NSDate *created;
@property (nonatomic, strong) NSNumber *age;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSDate *updated;
@property (nonatomic, strong) NSString *objectId;
@property (nonatomic, strong) NSString *phone;
@property (nonatomic, strong) Address *homeAddress;
@end
  • PhoneBook.h:
#import <Foundation/Foundation.h>

@class Contact;

@interface PhoneBook : NSObject
@property (nonatomic, strong) NSString *objectId;
@property (nonatomic, strong) NSDate *updated;
@property (nonatomic, strong) NSString *ownerId;
@property (nonatomic, strong) NSDate *created;
@property (nonatomic, strong) Contact *owner;
@property (nonatomic, strong) NSMutableArray *contacts;

-(void)addToContacts:(Contact *)contact;
-(void)removeFromContacts:(Contact *)contact;
-(NSMutableArray *)loadContacts;
-(void)freeContacts;
@end

I didn’t find any #include … directive in .h files, only @class
Please, provide your classes, which demonstrated this issue, we can’t reproduce it.

I just regenerated the model files from code generation and I see the same result as you now with no imports. I previously generated them about 10 days ago.

Thanks

Ok, we are going to add the collection generics in a codegen section in our next release. Thank you for your recommendations.

To note on this, it seems that the code generation is different between the general code generation section and the code generation when selected from a custom business logic deployment. General code generation uses @class but the custom business logic code generation uses #import

Custom business logic code generator cannot produce objective-c, it creates only Java, PHP and JavaScript.

I mean from viewing the custom business logic deployed hosted service where you can download the service SDKs:

Download Service SDKs: https://develop.backendless.com/images/ANDROID-sk.png</img> Android/Java https://develop.backendless.com/images/IOS-sk.png</img> Obj-C/Swift https://develop.backendless.com/images/JS-sk.png</img> JavaScript https://develop.backendless.com/images/AS-sk.png</img> ActionScript https://develop.backendless.com/images/WP-sk.png</img> .NET

Thanks for clarifying. You’re talking about the generated SDKs for your API Engine service - the option available under the “Hosted Services” menu. I was referring to event handlers and timers.