Skip to content
Advertisement

class and extending interface in objective c

I know bit of ts and I am new to Objective C.

I have a meeting param which I want to extend in meeting obj (MeetingConfig.h) file

@interface MeetingParams : NSObject
  @property (nonatomic, assign) NSString* roomName;
  @property (nonatomic, assign) NSString* authToken;
  @property (nonatomic, assign)Boolean autoTune;
  @property (nonatomic, assign)NSString* apiBase;
  @property (nonatomic, assign)Boolean showSetupScreen;
@end

@interface MeetingConfig : NSObject
@property (nonatomic, assign) NSString* roomName;
@property (nonatomic, assign) NSString* authToken;
@property (nonatomic, assign)Boolean autoTune;
@property (nonatomic, assign)NSString* apiBase;
@property (nonatomic, assign)Boolean showSetupScreen;

- (void) setAuthToken:(NSString *)authToken;
- (void) setApiBase:(NSString *)apiBase;
- (void) setShowSetupScreen:(Boolean)showSetupScreen;
- (void) setAutoTuneEnabled:(Boolean)autoTune;
- (id) init;
@end

How can I do that? (right now I know there is redundant code)

Also, I know I am mixing language but can someone tell me what would be the equivalence of this code from Java in objective C?

 MeetingConfig config = new MeetingConfig();
        config.setRoomName("abc");

For now, I have made this as MeetingConfig.m file

#import "MeetingConfig.h"

@implementation MeetingConfig

- (id) init
{
  if (self = [super init]) {
    self.apiBase = @"https://api.xyz.in";
    self.showSetupScreen = false;
    self.autoTune = true;
  }
  return self;
}

- (void) setAuthToken:(NSString *)authToken
{
  self.authToken = authToken;
}
- (void) setApiBase:(NSString *)apiBase
{
  self.apiBase = apiBase;
}
- (void) setShowSetupScreen:(Boolean)showSetupScreen
{
  self.showSetupScreen = showSetupScreen;
}
- (void) setAutoTuneEnabled:(Boolean)autoTune
{
  self.autoTune = autoTune;
}

@end

Which I am hoping is equivalent to my this file

package com.dyteclientmobile;

public class MeetingConfig {
    public String roomName;
    public String authToken;
    public boolean autoTune;
    public String apiBase;
    public boolean showSetupScreen;

    public MeetingConfig() {
        this.apiBase = "https://api.xyz.in";
        this.showSetupScreen = false;
        this.autoTune = true;
    }

    public MeetingConfig setRoomName(String roomName) {
        this.roomName = roomName;
        return this;
    }

    public MeetingConfig setAuthToken(String authToken) {
        this.roomName = roomName;
        return this;
    }

    public MeetingConfig setApiBase(String apiBase) {
        this.apiBase = apiBase;
        return this;
    }

    public MeetingConfig setShowSetupScreen(boolean showSetupScreen) {
        this.showSetupScreen = showSetupScreen;
        return this;
    }

    public MeetingConfig setAutoTuneEnabled(boolean autoTune) {
        this.autoTune = autoTune;
        return this;
    }
}

Advertisement

Answer

Just for the MeetingConfig definition, you can do something like, in MeetingConfig.h:

#import <Foundation/Foundation.h>

@interface MeetingConfig : NSObject

@property(nonatomic, assign, readonly) NSString *roomName;
@property(nonatomic, assign, readonly) NSString *authToken;
@property(nonatomic, assign, readonly) BOOL autoTune;
@property(nonatomic, assign, readonly) NSString *apiBase;
@property(nonatomic, assign, readonly) BOOL showSetupScreen;

- (id)init;
- (MeetingConfig *)setAuthToken:(NSString *)authToken;
- (MeetingConfig *)setRoomName:(NSString *)roomName;
- (MeetingConfig *)setApiBase:(NSString *)apiBase;
- (MeetingConfig *)setShowSetupScreen:(BOOL)showSetupScreen;
- (MeetingConfig *)setAutoTuneEnabled:(BOOL)autoTune;

@end

and, in MeetingConfig.m:

#import "MeetingConfig.h"

@implementation MeetingConfig

- (id)init {
  if (self = [super init]) {
    _apiBase = @"https://api.xyz.in";
    _showSetupScreen = false;
    _autoTune = true;
  }
  return self;
}

- (MeetingConfig *)setAuthToken:(NSString *)authToken {
  _authToken = authToken;
  return self;
}

- (MeetingConfig *)setRoomName:(NSString *)roomName {
  _roomName = roomName;
  return self;
}

- (MeetingConfig *)setApiBase:(NSString *)apiBase {
  _apiBase = apiBase;
  return self;
}

- (MeetingConfig *)setShowSetupScreen:(BOOL)showSetupScreen {
  _showSetupScreen = showSetupScreen;
  return self;
}

- (MeetingConfig *)setAutoTuneEnabled:(BOOL)autoTune {
  _autoTune = autoTune;
  return self;
}

@end

Finally, an example of using this:

#import "MeetingConfig.h"

void printConfig(MeetingConfig *);

int main(int argc, char **argv) {
  MeetingConfig *config = [[MeetingConfig alloc] init];
  printConfig(config);
  [[[config setRoomName:@"abc"] setAuthToken:@"def"] setApiBase:@"http://localhost:8080"];
  printConfig(config);
}

void printConfig(MeetingConfig *config) {
  printf("roomName:         %sn", [config.roomName UTF8String]);
  printf("authToken:        %sn", [config.authToken UTF8String]);
  printf("autoTone:         %dn", config.autoTune);
  printf("apiBase:          %sn", [config.apiBase UTF8String]);
  printf("showSetupScreen:  %dn", config.showSetupScreen);
}

The important differences between this and what you had put together:

  • Addition of the readonly attribute to each property, to ensure the corresponding set* methods weren’t synthesised, as they would conflict with the fluent versions
  • Modification to init and setters to update the instance variables directly
  • Changing the signature and implementation of each setter method to return a pointer to self, making them fluent

You’ll need to take a look a the documentation for @property, and other sources to get a sense of which attributes need to be set, I don’t have enough experience to be absolutely sure on that front.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement