Skip to content

Instantly share code, notes, and snippets.

@meech-ward
Last active October 28, 2016 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meech-ward/6214aca5a68574c7102596bed077b11d to your computer and use it in GitHub Desktop.
Save meech-ward/6214aca5a68574c7102596bed077b11d to your computer and use it in GitHub Desktop.

#iOS Interview Questions (Thermometer)

Here’s a model of a thermometer as a class and a struct:

public class ThermometerClass {
    private(set) var temperature: Double = 0.0
    public func register(temperature: Double) {
        self.temperature = temperature
    }
}

let thermometerClass = ThermometerClass()
thermometerClass.register(temperature: 56.0)

public struct ThermometerStruct {
    private(set) var temperature: Double = 0.0
    public mutating func register(temperature: Double) {
        self.temperature = temperature
    }
}

let thermometerStruct = ThermometerStruct()
thermometerStruct.register(temperature: 56.0)

This code fails to compile. Where? Why?

Here's the same code in Objective-C

@interface Thermometer : NSObject

@property (nonatomic, strong, readonly) NSNumber *temperature;

- (void)registerTemperature:(NSNumber *)temperature;

@end
@interface Thermometer()

@property (nonatomic, strong, readwrite) NSNumber *temperature;

@end

@implementation Thermometer

- (void)registerTemperature:(NSNumber *)temperature {
    self.temperature = temperature;
}

@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment