Swift provides a special preprocessor directive called os to check what operating system you are running:
struct A {
#if os(iOS)
let a: AvailableOnIOS
#elseif os(tvOS)
let a: AvailableOnTVOS
#endif
}
I am interested if there is something similar in Objective-C that can be used like
#interface A: NSObject
#if os(iOS)
#property (nonatomic, strong) AvailableOnIOS* a;
#elseif os(tvOS)
#property (nonatomic, strong) AvailableOnTVOS* a;
#endif
#end
Related
I am a beginner in objective C and I am using the following code
I am using xcode6
// Car.h
#import <Foundation/Foundation.h>
#interface Car : NSObject {
NSString *simple_name; //This is private
#property int mssp; //Cant use #property Error illegal visibility specification
}
-(void) sayHi:(NSString*)msg ;
#end
Any suggestions on why I am getting this error ?
Move #property int mssp; out of the brackets:
// Car.h
#import <Foundation/Foundation.h>
#interface Car : NSObject {
NSString *simple_name; //This is private
}
#property int mssp;
-(void) sayHi:(NSString*)msg ;
#end
The internal(set) access modifier in Swift allows a property to be changed within the same module, but not from the outside. I'm curious about whether it has an Objective-C equivalent, and how I can implement it.
AFAIK, there is no equivalent in Objective-C.
But you can hide the setter outside from the module (Framework). For example:
MyObject.h: as Public header
#import <Foundation/Foundation.h>
#interface MyObject : NSObject
// `readonly` for public
#property (strong, nonatomic, readonly) NSString *myProp;
#end
MyObject-Internal.h: as Project header
#import "MyObject.h"
#interface MyObject ()
// NOT `readonly` for internal
#property (strong, nonatomic) NSString *myProp;
#end
Then, you can use MyObject-Internal.h in .m codes inside the module.
I am getting the following error
Expected '{' before 'extern'
while using a third party header file and it has the following code
extern NSString *const kXXXError;
Is the usage related to Xcode version as currently i am using Xcode4
extern NSString *const kDeviceErrorDomain;
extern const NSInteger kDeviceErrorCodeUnknown;
#class DeviceInsightInternal;
#interface Device : NSObject {
DeviceInternal *_internal;
}
- (id)init;
- (NSString*)collect:(NSError**)error;
#property (nonatomic, assign) BOOL allowsJavascript;
#property (nonatomic, assign) BOOL privacyEnabled;
#end
You cannot declare a scalar property to have a any sort of retain,assign,copy semantic, it will result in a compile error. You should change:
#property(nonatomic, assign) BOOL allowsJavascript;
#property (nonatomic, assign) BOOL privacyEnabled;
to
#property(nonatomic) BOOL allowsJavascript;
#property (nonatomic) BOOL privacyEnabled;
So I have a class with a NSInteger in it and now I want to return the NSInteger value. For some kind of reason, the code for that is not working. I have already declared the #property for the NSInteger class.
#property (readwrite, assign, nonatomic) NSInteger numberFun;
- (NSInteger)sampleMethod {
...
return sample.numberFun;
}
The compiler says "Return from pointer without a cast". I'm pretty sure that means that I'm using a C type for an objective-c method. I want to know the work around for this. (Though I don't want it to return a casted NSInteger as a NSNumber).
Thanks
The following code sample compiles fine. I suggest you present a more complete example of your problem so we can figure out what you are doing wrong.
#interface MyObject : NSObject
{ }
#property (readwrite, assign, nonatomic) NSInteger numberFun;
#end
#implementation MyObject
#synthesize numberFun;
#end
#interface MyObject2 : NSObject
{ }
#property (nonatomic, copy) MyObject* sample;
#end
#implementation MyObject2
#synthesize sample;
- (NSInteger)sampleMethod { return sample.numberFun; }
#end
I am new to iphone development. I have this code in the delegate.h section:
#import <UIKit/UIKit.h>
#import <objc/Object.h>
#class Learning1ViewController;
#interface Greeter: NSObject<UIApplicationDelegate>
{
}
-(void)greet;
#end
#include <stdio.h>
#implementation Greeter
-(void) greet
{
printf ("Hello, World!\n");
}
#include <stdlib.h>
int main(void)
{
id myGreeter;
myGreeter=[Greeter new];
[myGreeter greet];
[myGreeter free];
return EXIT_SUCCESS;
}
#end
#interface Learning1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Learning1ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet Learning1ViewController *viewController;
#end
When i compile i get this error:
ld: duplicate symbol _main in /Users/ianbennett/Desktop/iphone development/Learning1/build/Learning1.build/Debug-iphonesimulator/Learning1.build/Objects-normal/i386/Learning1AppDelegate.o and /Users/ianbennett/Desktop/iphone development/Learning1/build/Learning1.build/Debug-iphonesimulator/Learning1.build/Objects-normal/i386/main.o
command/Developer/platforms/iphoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
I have seen that other people have had similar errors and that it might be to do with my library but im not really sure how to fix it.
You have defined the main() function twice (looks like it's defined in Learning1AppDelegate.m and main.m
Thanks for the error message
You have defined the same function main in 2 places
The files are
main
Learning1AppDelegate
You can only define a function in one place - so you have to choose