How to convert this code from Swift to Objective C - objective-c

private var distance: Double {
return fabs(max - min)
}
How to define this variable in Objective C language.

In your .m file:
#interface MyClass ()
#property (nonatomic, assign, readonly) double distance;
#end
#implementation MyClass
-(double)distance {
return fabs(max - min);
}
#end

Related

Could not cast value of type - Objective C NSManagedObject in Swift

I've found numerous posts about this error being able to resolve by adding the prefix of the project name to the class in entity viewer e.g ProjectName.Journey also the suggestion of adding #objc(ClassName) above the class declaration. But these solutions don't work for me. The problem is I have a NSManagedObject class generated in Objective C that I then try to access in Swift. I get the following error:
Unable to load class named 'ProjectName.Journey' for entity 'Journey'. Class not found, using default NSManagedObject instead.
Could not cast value of type 'NSManagedObject_Journey_' (0x1700541f0) to 'Journey' (0x100096f38).
My Class:
H:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Journey : NSManagedObject
#property (nonatomic, retain) NSString * travelMode;
#property (nonatomic, retain) NSDate * startTime;
#property (nonatomic, retain) NSDate * endTime;
#property (nonatomic, retain) NSNumber * averageSpeed;
#property (nonatomic, retain) NSNumber * distance;
#end
M:
#import "Journey.h"
#implementation Journey
#dynamic travelMode;
#dynamic startTime;
#dynamic endTime;
#dynamic averageSpeed;
#dynamic distance;
#end
My code trying to access this code in Swift:
for res in results {
var move: Journey = res as! Journey; //error thrown here
var morningStamp = midnightDate!.timeIntervalSince1970;
var eveningStamp = morningStamp + 86400;
if(move.startTime.timeIntervalSince1970 > morningStamp) {
if(move.endTime.timeIntervalSince1970 < eveningStamp) {
filtered.addObject(move);
}
}
}
bridge header file:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "TravelRecordManager.h"
#import "Journey.h"

Objective-C public get set method for private property

I wonder if it is possible to use #synthesize on a private #property so that the get/set methods have public access.
Right now my code looks something like this
SomeClass.h
#import <Foundation/Foundation.h>
#interface SomeClass : NSObject
{
#private
int somePrivateVariable;
}
#end
SomeClass.m
#import "SomeClass.h"
#interface SomeClass ()
#property int somePrivateVariable;
#end
#implementation
#synthesize somePrivateVariable;
#end
Then in some outside function I want to be able to write:
#import "SomeClass.h"
SomeClass *someClass = [[SomeClass alloc] init];
[someClass setSomePrivateVariable:1337]; // set the var
NSLog("value: %i", [someClass getSomePrivateVariable]); // get the var
I know that I can just create my own get/set methods in the header file but I would enjoy using the #synthesize very much more.
If you want a public property to mirror a private one, just override the public property's getter and setter and return the private one.
#interface Test : NSObject
#property NSObject *publicObject;
#end
Then, in the implementation:
#interface Test ()
#property NSObject *privateObject;
#end
#implementation Test
- (NSObject *)publicObject
{
return self.privateObject;
}
- (void)setPublicObject:(NSObject *)publicObject
{
self.privateObject = publicObject;
}
#end

How to use enum in init method?

I have the following code which has an init method that takes an enum as an attribute, but I'm getting an error for an elected identifier.
typedef NS_ENUM(NSUInteger, ActivtyLevel) {
kActivityLevelSedentary,
kActivityLevelLight,
kActivityLevelModerate,
kActivityLevelHeavy,
kActivityLevelExtreme
};
#interface DFUserProfile ()
#property (nonatomic, strong) NSNumber *weight;
#property (nonatomic, strong) NSNumber *bodyFatPercentage;
#end
#implementation DFUserProfile
- (id)initWithWeight:(NSNumber *)iWeight bodyFat:(NSNumber *)iBodyFat andActivityLevel:(NSUInteger)iActivtyLevel {
if (self = [super init]) {
_weight = iWeight;
_bodyFatPercentage = iBodyFat;
ActivtyLevel = iActivtyLevel;
}
return self;
}
#end
The typedef declares a type called ActivityLevel, you need a variable or property with that type.
For example you might declare the property:
#property (nonatomic) ActivityLevel activityLevel;
and then declare your method as:
- (id)initWithWeight:(NSNumber *)iWeight
bodyFat:(NSNumber *)iBodyFat
andActivityLevel:(ActivityLevel)iActivtyLevel // note use of the enumeration type
{
...
_activityLevel = iActivityLevel;
You need to have a variable of the type of the enum, you can't store it directly into the enum, that is a type. Add this:
#property (nonatomic) ActivityLevel activityLevel;
then in your init method:
_activtyLevel = iActivtyLevel;

Solve pythagoras theorem in Objective-C

I am working on a programm which lets you calculate the sides in a triangle using the Pythagoras method. I recently posted my first question about this. How can I code it so that you can also work out B or A. Here is my code so far:
PythagorasCalc.m
#import "PythagorasCalc.h"
#implementation PythagorasCalc
- (double)calculatePythagorasValue {
return sqrt(A*A+B*B);
}
//Access Code
- (double)A {
return A;
}
- (void)setA: (double)value {
if(A != value) {
A = value;
}
}
- (double)B {
return B;
}
- (void)setB: (double)value {
if(B != value) {
B = value;
}
}
- (double)C {
return C;
}
- (void)setC: (double)value {
if(C != value) {
C = value;
}
}
#end
PythagorasCalc.h
#import <Cocoa/Cocoa.h>
#interface PythagorasCalc : NSObject {
#private
int A;
int B;
int C;
}
#property (nonatomic) double A;
#property (nonatomic) double B;
#property (nonatomic) double C;
- (double)calculatePythagorasValue;
#end
AppDelegate.m
#import "PythagorasCalculatorAppDelegate.h"
#implementation PythagorasCalculatorAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
calculator = [[PythagorasCalc alloc] init];
}
- (IBAction)calculateClicked:(id)sender {
double A = _ATextField.doubleValue;
double B = _BTextField.doubleValue;
double C = _CTextField.doubleValue;
calculator.A = A;
calculator.B = B;
calculator.C = C;
_CTextField.doubleValue = [calculator calculatePythagorasValue];
}
#end
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "PythagorasCalc.h"
#interface PythagorasCalculatorAppDelegate : NSObject <NSApplicationDelegate> {
//Public Calculator Object
PythagorasCalc *calculator;
}
#property (assign) IBOutlet NSWindow *window;
- (IBAction)calculateClicked:(id)sender;
#property (weak) IBOutlet NSTextField *ATextField;
#property (weak) IBOutlet NSTextField *BTextField;
#property (weak) IBOutlet NSTextField *CTextField;
#end

Global Variables in Objective C

I have a counter which I use to get an object at that counters index and I need to access it in another class.
How are static variables declared in Objective C?
Rather than make it global, give one class access to the other class's counter, or have both classes share a third class that owns the counter:
ClassA.h:
#interface ClassA {
int counter;
}
#property (nonatomic, readonly) int counter;
ClassA.m
#implementation ClassA
#synthesize counter;
ClassB.h:
#import "ClassA.h"
#interface ClassB {
ClassA *a;
}
ClassB.m:
#implementation ClassB
- (void)foo {
int c = a.counter;
}
Hi alJaree,
You declare a static variable in the implementation of Your class and enable access to it through static accessors:
some_class.h:
#interface SomeClass {...}
+ (int)counter;
#end
some_class.m:
#implementation SomeClass
static int counter;
+ (int)counter { return counter; }
#end