I am writing a unit test to test a method that updates a checklist. The checklist has these properties:
typedef NS_ENUM (NSUInteger, ChecklistStatus) { Pending, Completed };
#protocol IChecklistItem <NSObject>
#property (nonatomic, assign, readonly) NSInteger Id;
#property (nonatomic, copy, readonly) NSString *Description;
#property (nonatomic, assign, readonly)BOOL IsCompleted;
#property (nonatomic, assign, readwrite) ChecklistStatus Status;
#property (nonatomic, strong, readwrite) NSDate *CompletedDate;
#property (nonatomic, copy, readwrite) NSString *CompletedByUserId;
#property (nonatomic, assign, readonly) NSInteger RoleId;
#property (nonatomic, assign, readonly) NSInteger GroupId;
#property (nonatomic, strong, readonly) NSArray<IChecklistNote> *Notes;
- (void)sortNotes;
#end
However, in my unit test, as I am trying to validate,
checklistItem.Description = #"hello";, I get the error"Assignment to readonly property"
Why is this so?
heres the rest of my test method:
- (void)testUpdateChecklist {
NSString *testChecklistId = #"1";
NSString *testPatientDescription = #"Descriptive Description";
// What other properties do I need here?
XCTAssertNotNil(_service);
__block CCChecklistItem *checklistItem = nil;
SignalBlocker *blocker = [[SignalBlocker alloc] initWithExpectedSignalCount:1];
id delegate = OCMProtocolMock(#protocol(ChecklistServiceDelegate));
OCMExpect([delegate didCompleteUpdateChecklistItem:[OCMArg checkWithBlock:^BOOL(id obj) {
checklistItem = obj;
XCTAssertNotNil(checklistItem);
[blocker signal];
return true;
}]]);
[_service updateChecklistItem:checklistItem delegate:delegate];
[blocker waitWithTimeout:5.0f];
OCMVerifyAll(delegate);
NSString *originalDescription = checklistItem.Description;
checklistItem.Description = #"hello";
}
EDITED QUESTION:
So when I change the property from above to ReadWrite, I get this error in CChecklistItem
#interface CCChecklistItem ()
#property (nonatomic, assign, readwrite) NSInteger Id;
#property (nonatomic, copy, readwrite) NSString *Description;
#property (nonatomic, assign, readwrite) NSInteger RoleId;
#property (nonatomic, assign, readwrite) NSInteger GroupId;
#property (nonatomic, strong, readwrite) NSMutableArray<IChecklistNote> *Notes;
#end
`Illegal redeclaration of readwrite property in class extension 'CChecklistItem'
Your property is set to readonly as seen here:
#property (nonatomic, copy, readonly) NSString *Description;
Change it to:
#property (nonatomic, copy) NSString *Description;
or if you want to be consistent with the other properties (though overly explicit, IMO):
#property (nonatomic, copy, readwrite) NSString *Description;
Changing scope visibility only to satisfy tests is not encouraged. The easiest solution in your case would be to take advantage of wonderful KVO which Objective-C gives you.
Translated to your original question it would be something like:
[checklistItem setValue:#"hello" forKey:#"Description"]
No need to change access modifiers and your tests will be fine.
Your property is declared readonly in the protocol that the class CChecklistItem conforms. When that property is then synthersized it will create the backing variable and a getter method -(NSString *)description; but no setter method, since it is readonly. So redeclaring it as readwright in your anonymous category, that i'm guessing is declared in your test file to expose private methods to the test case, won't work since there still is no setter method for the property. Further more, even if you decide to try to make your own setter in the implementation of a category on your class you can't since there is no way to access the variable _description that is only exposed in the CChecklistItem.m file.
Depending on what you need to do with your test it might work to stub the getter - (NSString *)description; and return your #"hello" string when that method is called instead of trying to set the actual value to the backing variable.
Generally, we delare property like this:
#Property (nonatomic, retain) NSString * string;
#property (nonatomic, assign) NSInteger number;
But if I want to declare a CF object, how can I do?
Is it the same under ARC?
#property(nonatomic, strong) __attribute__((NSObject)) CFStringRef myString;
This way, ARC will do all the work for you.
Because there is tollfree bridging between NSString and CFStringRef, you could also do:
#property (nonatomic, strong) NSString *myString;
and when setting the property with a CFStringRef:
CFStringRef myStringRef = CFSTR("Hello!");
myObject.myString = (__bridge_transfer NSString *)myStringRef;
See also: http://amattn.com/2011/12/07/arc_best_practices.html
and: assign properties, ARC and Core Foundation objects
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;
This code works:
#interface StringStuff : NSObject {
}
#property (nonatomic, retain) NSString *String1;
#property (nonatomic, retain) NSString *String2;
- (NSString *) doSomethingWithStrings;
#end
But I often see:
#interface StringStuff : NSObject {
NSString *String1;
NSSTring *String2;
}
#property (nonatomic, retain) NSString *String1;
#property (nonatomic, retain) NSString *String2;
- (NSString *) doSomethingWithStrings;
#end
Is there a reason that properties are often declared as an instance variable as well? Is it just considered good form?
Legacy; it used to be (and still is on 32 bit Mac OS X targeted code) that the ivar declarations were required. That is no longer true on iOS, the simulator and 64 bit OS X.
Note that it is common to #synthesize iVar = iVar_; to prevent accidental direct access where self.iVar is really required.
i have this variables (int and double-array)
.h-File
#interface MyCLass : NSObject
{
int myInt;
double paramStack[100];
}
#property (nonatomic, assign) int myInt;
//#property (nonatomic, assign) double paramStack; //<- ?
.m-File
#synthesise myInt;
//#synthesize paramStack; //<- ?
I want the int and the double-array-variable accessible from other classes via properties.
For the int-var. it looks fine, but the array throws errors at .m-file (#synthsize) and at h.file (#property (nonatomic, assign) double paramStack).
How can i define
"#property (nonatomic, assign) double paramStack;" as a double-array?
Thanks
Make the property with a pointer:
#property(nonatomic, assign) double *paramStack;
You can just use it like this:
NSLog(#"%f", self.paramStack[20]);
This is mainly because an array cannot be returned, but a pointer can. I.E. this getter would be impossible and that's why you cannot create an array property:
- (double[100])paramStack;