I have the following enum:
typedef NS_ENUM(NSUInteger, GraphType) {
GraphTypeRawData,
GraphTypeFilteredData
};
The compiler accepts without warning me declaring it as a property primitive, or as a pointer:
#property (nonatomic, assign) GraphType graphType;
VS
#property (nonatomic, assign) GraphType *graphType;
Which is the correct one to use? (And why?)
It's a primitive type. Don't use a pointer unless you have a very clear and specific reason to track a pointer to a primitive type (which will be very rare).
Related
hi iam learning IOS development & for learning porpoises
i want to know how to make this property nullable in objc
(this Four #property)
#property (nonatomic) BOOL Hood;
#property (nonatomic) BOOL smartclean;
#property (nonatomic) t_ShirtSize size;
#property (nonatomic) NSUInteger newsoftness;
because
when i use nullable i get this Error
#property (nullable,nonatomic) BOOL Hood;
Nullability specifier 'nullable' cannot be applied to non-pointer type 'BOOL' (aka 'signed char')
what is the Right way to make all 4 property nullable how can i do that (accept Null)
this is M file for The My app
-(instancetype _Nonnull) initWithSize:(t_ShirtSize)size
Hood: (BOOL)hoody {
self = [super init];
if(self) {
Hood = hoody;
_size = size;
}
return self;
}
If you absolutely need a nullable BOOL, you can use NSNumber as a wrapper:
#property (nullable,nonatomic) NSNumber* Hood;
use Hood = #YES; /* or #NO */ to assign a value, and Hood.boolValue to read the value again.
Note that, as others have said already, this is not a full replacement for Swift's optionals, and you have to take extra care to always use the correct data type - the compiler won't help you if you accidentally store a floating point value in your "nullable NSUInteger".
A BOOL is a primitive, not an object. If you need an object, the usual thing is to wrap your BOOL in an NSNumber:
https://developer.apple.com/documentation/foundation/nsnumber/1415728-initwithbool?language=objc
https://developer.apple.com/documentation/foundation/nsnumber/1410865-boolvalue?language=objc
Now you have an NSNumber, so you have something that can be nil to indicate there is no object there.
The nullable annotation helps Swift understand whether a reference is optional or not when Objective-C code interfaces with Swift. It doesn't change the operation of Objective-C in any way.
nil can be assigned to a pointer in Objective-C even if it doesn't have a nullability annotation; that is just the nature of C pointers.
Scalar value types (Integers, bools etc) cannot be null in Objective-C, they always have a value since there is no pointer to their value. Their value is stored directly.
There is no direct Objective-C support for optional or nullable value types as there is in Swift
In Swift, an optional is actually a struct containing the wrapped value and a property that indicates whether the optional has a value or not. The compiler hides this detail from you.
You could use NSNumber. This wraps scalar value types in an object. The object reference can be nil and so is nullable
#property (nonatomic, nullable) NSNumber* Hood;
#property (nonatomic, nullable) NSNumber* smartclean;
#property (nonatomic) t_ShirtSize size;
#property (nonatomic, nullable) NSNumber* newsoftness;
You don't need that nullability specifier anymore with BOOL properties, and perhaps also for the NSUInteger.
For BOOL, the reason why you don't need it is that it's by default 0 or <nil>.
Is it possible to use an Enum as a property for my model? I currently have a class like this:
typedef NS_ENUM(NSUInteger, ListType) {
ListTypeDay,
ListTypeWeek,
ListTypeMonth,
ListTypeYear,
ListTypeCustom
};
#interface ListItem : RLMObject;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, assign) ListType itemType;
#property (nonatomic, assign) BOOL isFinish;
#property (nonatomic, assign) NSTimeInterval targetTime;
#end
RLM_ARRAY_TYPE(ListItem)
Terminal output:
Terminating app due to uncaught exception 'RLMException', reason: 'Can't persist property 'itemType' with incompatible type. Add to ignoredPropertyNames: method to ignore.'
No, you can't store custom types (enums included) in Realm. See Supported Types in the documentation.
Realm supports the following property types: BOOL, bool, int, NSInteger, long, long long, float, double, NSString, NSDate, NSData, and NSNumber tagged with a specific type.
Simply replace NSUInteger to NSInteger in type definition.
typedef NS_ENUM(NSInteger, ListType)
My understanding so far is that (retain) increases the reference count of a property and is essentially the exact same as (strong). Since all properties are set to retain by default (unless specified otherwise), is adding (strong) needed at all:
#property(nonatomic, strong) NSString *name;
Is the same as:
#property(nonatomic) NSString *name;
Both the above are the same, right?
Since ARC was introduced, "strong", "atomic", and "readwrite" are set by default.
These properties are equivalent:
#property NSArray *name;
#property (strong, atomic, readwrite) NSArray *name;
Source: http://useyourloaf.com/blog/default-property-attributes-with-arc.html
From the documentation:
By default, both Objective-C properties and variables maintain strong
references to their objects.
So both forms are the same.
What is the difference between these two declarations in Objective-C?
I have been looking at some Apple source code example and they used the second one in various circumstances. I just wanted to understand why and when is best to use the second version rather than the first one (I know the difference between strong, weak, atomic, nonatomic).
#property(nonatomic, strong) NSObject * myObject;
// vs
#property NSObject * myObject2; //No additional qualifiers
#property NSObject * myObject2
// is same as
#property (atomic,strong) NSObject * myObject2
which one to use, is developer's personal choice
I'm new to Objective-C and I'm having trouble with the whole nonatomic, strong, weak, etc. I'm wondering if I will have any issues using Core Data with float values which are defined like so:
#property (nonatomic) float * rating;
#property (nonatomic) float * mRating;
Should I declare the differently?
Yes, you should declare them without asterisks:
#property (nonatomic) float rating;
#property (nonatomic) float mRating;
Asterisks indicate pointers. All Objective C classes are declared with asterisks, because instances are referred to through pointers. Primitives such as floats, ints, etc. are defined as values, i.e. without asterisks. Same goes for typedef-ed types such as CGFloat and NSInteger: scalar fields of these types should be defined without an asterisk.
You should definitely lose the *, unless you are meaning to create a pointer. Outside of that it looks great!