What is the difference between STAssertEqualObjects and STAssertEquals? - objective-c

I have the following class:
#import "Period.h"
#implementation Period
...
- (BOOL)isEqualTo:(id)object {
return [self isEqual:object];
}
- (BOOL)isEqual:(id)object {
if (object == self) {
return YES;
}
if ([[object beginDate] hash] == [[self beginDate] hash] &&
[[object endDate] hash] == [[self endDate] hash]) {
return YES;
}
return NO;
}
...
#end
And also the following test, written using OCUnit:
Period *period;
NSDate *beginDate;
NSDate *endDate;
- (void)setUp {
beginDate = [NSDate dateWithString:#"2011-02-25"];
endDate = [NSDate dateWithString:#"2011-03-25"];
period = [[Period alloc] initWithBeginDate:beginDate
endDate:endDate];
}
- (void)testEndDateShouldBeGreaterOrEqualThanBeginDate {
Period *newPeriod = [[Period alloc] initWithBeginDate:beginDate
endDate:beginDate];
STAssertEqualObjects(beginDate, [newPeriod beginDate], #"Begin dates are different");
STAssertEqualObjects(endDate, [newPeriod endDate], #"End dates are different");
}
Previously I was using STAssertEquals instead of STAssertEqualObjects and it was not calling isEqual method on Period.
I just want to understand two things:
What's the difference between those two methods?
What's the difference between isEqual and isEqualTo?

STAssertEquals compares the raw bytes that make up the two parameters that are passed to it, and is intended to be used with scalar types (float, int, char, etc.), structs or unions---you should not use it to compare Objective-C objects. STAssertEqualObjects compares two Objective-C objects by calling isEqual:.
isEqualTo: is used to support NSSpecifierTest (refer to the NSComparisonMethods Protocol Reference). It is unnecessary to provide an implementation for isEqualTo: if your objects aren't scriptable.

Related

Building 3 methods in Objective C

I'm having trouble with a school problem in Objective C. I need to build 3 methods. The first method tells you if someone is in a line. If nobody is in the line it tells you nobody is in the line otherwise it tells you who is in the line and it lists the names on a new line.
The second method adds names to the line.
The third method removes a name from the line and tells you who was removed.
First method:
-(NSString*)stringWithDeliLine:(NSArray*) deliLine{
NSString *empty = #"The line is currently empty.";
//Some kind of formatted string
if(deliLine == nil || [deliLine count] == 0)
{
empty;
}
else
{
//formatted string
}
//not sure how to return either empty or formatted string
}
Second Method:
-(void)addName:toDeliLine:(NSString*)name:(NSMutableArray*)deliLine{
[deliLine addObject:name];
}
The third method I was going to use removeObject but the instructions said not to use it so I have no idea where to start.I have the signature I think.
-(NSString*)serveNextCustomerInDeliLine:(NSMutableArray*)deliLine{
return nil;
}
For the first method I'm not sure why my literal string won't work in the if statement. I thought I was saying look at the array if nothing is in the array then it's the first object and show the string literal. else show some kinda of formatted string. I've tried all kinds of strings but none seem to be working so that's why I have the comment formatted string. If someone could give me a hint that would be great. I don't need the answer just a clue on what to think about. This is long post sorry.
A possible implementation can be the following. Please note that I have not testes edge cases and I wrote the code without Xcode support
#import <Foundation/Foundation.h>
#interface Line : NSObject
- (NSString*)printLine;
- (void)addCustomer:(NSString*)customer;
- (NSString*)removeCustomer:(NSString*)customer;
#end
#import "Line.h"
#interface Line ()
#property (nonatomic, strong, readwrite) NSMutableArray<NSString*> *customers;
#end
#implementation Line
- (instancetype)init {
self = [super init];
if (self) {
_customers = [NSMutableArray array];
}
return self;
}
- (NSString*)printLine {
NSUInteger count = self.customers.count;
if(count == 0) {
return #"Empty";
}
NSMutableString *descr = [NSMutableString string];
for (NSString *customer in self.customers) {
[descr appendString:[NSString stringWithFormat:#"%# ", customer]];
}
return [descr copy];
}
- (void)addCustomer:(NSString*)customer {
[self.customers addObject:customer];
}
- (NSString*)removeCustomer:(NSString*)customer {
NSUInteger index = [self.customers indexOfObject:customer];
if(index == NSNotFound) {
return [NSString stringWithFormat:#"%# not removed", customer];
}
NSString *removedCustomer = [self.customers objectAtIndex:index];
[self.customers removeObjectAtIndex:index];
return removedCustomer;
}
#end
Usage:
Line *line = [[Line alloc] init];
[line addCustomer:#"customer"];
NSLog(#"%#", [line printLine]);
NSLog(#"%#", [line removeCustomer:#"customer"]);
NSLog(#"%#", [line printLine]);
Edit:
I've updated my answer, passing the array as a parameter is not necessary, just initialize deliLine as a mutable array property.
For you first method, you could do the following,
- (NSString *)deliLineContents {
NSString *empty = #"The line is currently empty.";
NSMutableString *namesInQueue = [[NSMutableString alloc] init];
if(self.deliLine == nil || [self.deliLine count] == 0) {
return empty;
} else {
// Loop through your array and return a string of all the names
for (NSString *string in self.deliLine ) {
[namesInQueue appendString:string];
}
}
return [NSString stringWithString:namesInQueue];
For your second method, you're already pretty much there, maybe look up how to construct method signatures.
- (void)addNameToDeliLine:(NSString*)name {
[self.deliLine addObject:name];
}
For your third method, not sure if this meets your requirement, if not let me know.
- (NSString *)customerRemovedFromLine {
// I've making an assumption that you want to remove the first customer
NSString *servedCustomer = [self.deliLine objectAtIndex:0];
[self.deliLine removeObjectAtIndex:0];
return servedCustomer;
}
You probably don't need to pass deliLine around, just create it as a property and access it with self.deliLine. Anyway hope this helps, good luck.

Create a NSSet from NSArray based on property

How does one create a NSSet of objects from an array based on a property.
e.g. Array of objects, each with a strong reference to a type property, and multiple occurrences of each type exist in the array. How can this be turned into an NSSet holding a single object of each type.
NSSet *distinctSet = [NSSet setWithArray:[array valueForKeyPath:#"#distinctUnionOfObjects.property"]];
A dictionary essentially has this functionality already. Its keys are a set, so you can create the dictionary to hold the objects, keyed by whatever attribute you're interested in:
[NSDictionary dictionaryWithObjects:arrayOfObjects
forKeys:[arrayOfObjects valueForKey:theAttribute]];
If you ask the dictionary for allValues now, you have only one object for each attribute. I should mention that with this procedure, the later objects will be kept in favor of earlier. If the order of your original array is significant, reverse it before creating the dictionary.
You can't actually put those objects into an NSSet, because the NSSet will use the objects' isEqual: and hash methods to determine whether they should be members, rather than the key attribute (of course, you can override these methods if this is your own class, but that would likely interfere with their behavior in other collections).
If you really really feel that you need a set, you will have to write your own class. You can subclass NSSet, but conventional wisdom is that composition of Cocoa collections is far easier than subclassing. Essentially, you write a class which covers any set methods you're interested in. Here's a (quite incomplete and totally untested) sketch:
#interface KeyedMutableSet : NSObject
/* This selector is performed on any object which is added to the set.
* If the result already exists, then the object is not added.
*/
#property (assign, nonatomic) SEL keySEL;
- (id)initWithKeySEL:(SEL)keySEL;
- (id)initWithArray:(NSArray *)initArray usingKeySEL:(SEL)keySEL;
- (void)addObject:(id)obj;
- (NSArray *)allObjects;
- (NSArray *)allKeys;
- (BOOL)containsObject:(id)obj;
- (NSUInteger)count;
-(void)enumerateObjectsUsingBlock:(void (^)(id, BOOL *))block;
// And so on...
#end
#import "KeyedMutableSet.h"
#implementation KeyedMutableSet
{
NSMutableArray * _objects;
NSMutableSet * _keys;
}
- (id)initWithKeySEL:(SEL)keySEL
{
return [self initWithArray:nil usingKeySEL:keySEL];
}
- (id)initWithArray:(NSArray *)initArray usingKeySEL:(SEL)keySEL
{
self = [super init];
if( !self ) return nil;
_keySEL = keySEL;
_objects = [NSMutableArray array];
_keys = [NSMutableSet set];
for( id obj in initArray ){
[self addObject:obj];
}
return self;
}
- (void)addObject:(id)obj
{
id objKey = [obj performSelector:[self keySEL]];
if( ![keys containsObject:objKey] ){
[_keys addObject:objKey];
[_objects addObject:obj];
}
}
- (NSArray *)allObjects
{
return _objects;
}
- (NSArray *)allKeys
{
return [_keys allObjects];
}
- (BOOL)containsObject:(id)obj
{
return [_keys containsObject:[obj performSelector:[self keySEL]]];
}
- (NSUInteger)count
{
return [_objects count];
}
- (NSString *)description
{
return [_objects description];
}
-(void)enumerateObjectsUsingBlock:(void (^)(id, BOOL *))block
{
for( id obj in _objects ){
BOOL stop = NO;
block(obj, &stop);
if( stop ) break;
}
}
#end
NSMutableSet* classes = [[NSMutableSet alloc] init];
NSMutableSet* actualSet = [[NSMutableSet alloc] init];
for(id object in array) {
if([classes containsObject:[object class]] == NO) {
[classes addObject:[object class]];
[actualSet addObject:object];
}
}
You would use:
NSSet* mySetWithUniqueItems= [NSSet setWithArray: yourArray];
This should work regardless of the type of objects in your array and would populate the NSSet with only one occurence of any duplicate objects in your array.
I hope this helps.
Update:
Next best thing: is use concatenation of class name and object property first then use the above method.
self.concatenatedArray=[NSMutableArray arrayWithCapacity:4];
for (TheClass* object in self.myArray)
[self.concatenatedArray addObject:[NSString stringWithFormat:#"%#-%#",[object class], object.theProperty]];
self.mySet=[NSSet setWithArray:self.concatenatedArray];
I am not sure what you will use the NSSet output for but you can probably modify the concatenation elements to have the information you need in the NSSet output.
I have created a simple library, called Linq to ObjectiveC, which is a collection of methods that makes this kind of problem much easier to solve. In your case you need the Linq-to-ObjectiveC distinct method:
NSSet* dictionary = [NSSet setWithArray:[sourceArray distinct:^id(id item) {
return [item type] ;
}]];
This returns a set where each item has a distinct type property.

How to Create hash from a class and selector? (objective c)

I have a dictionary that stores an object using a combination of the class name and selector as the key. I'm using the following function in order to calculate the hash:
+(NSString*) getKeyForClass:(Class) clazz andSelector:(SEL) selector {
return [NSString stringWithFormat:#"%#_%#",NSStringFromClass(clazz), NSStringFromSelector(selector)];
}
While running a profiler i've discovered that this function is the bottleneck of the computation. Is there a better (= more efficient) way to create a key from a class and a selector?
A few alternatives.
Keep using a string as a key, but do it faster:
Using a string is a bit more heavyweight than you really need, but it is at least simple.
Using -[NSString stringByAppendingString] would be faster. Parsing format strings is a lot of work.
return [[NSStringFromClass(clazz) stringByAppendingString:#"_"] stringByAppendingString:NSStringFromSelector(selector)];
It may be better to use a single NSMutableString instead of making intermediate strings. Profile it and see.
NSMutableString* result = [NSStringFromClass(clazz) mutableCopy];
[result appendString:#"_"];
[result appendString:NSStringFromSelector(selector)];
return result;
Use a custom object as a key:
You can make a custom object as the key that refers to the class and selector. Implement NSCopying and -isEqual: and -hash on it, so you can use it as a key in a dictionary.
#interface MyKey : NSObject <NSCopying>
{
Class _clazz;
SEL _selector;
}
- (id)initWithClass:(Class)clazz andSelector:(SEL)selector;
#end
#implementation MyKey
- (id)initWithClass:(Class)clazz andSelector:(SEL)selector
{
if ((self = [super init])) {
_clazz = clazz;
_selector = selector;
}
return self;
}
- (id)copyWithZone:(NSZone*)zone
{
return self; // this object is immutable, so no need to actually copy it
}
- (BOOL)isEqual:(id)other
{
if ([other isKindOfClass:[MyKey class]]) {
MyKey* otherKey = (MyKey*)other;
return _clazz == otherKey->_clazz && _selector == otherKey->_selector;
} else {
return NO;
}
}
// Hash combining method from http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))
- (NSUInteger)hash
{
return NSUINTROTATE([_clazz hash], NSUINT_BIT / 2) ^ (NSUInteger)_selector;
}
#end
+ (MyKey*)keyForClass:(Class)clazz andSelector:(SEL)selector
{
return [[MyKey alloc] initWithClass:clazz andSelector:selector];
}
Eliminate the middleman:
If you never need to pull the class and selector out of your key object, then you can just use the hash as computed above, stored in an NSNumber.
// Hash combining method from http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))
+ (NSNumber*)keyForClass:(Class)clazz andSelector:(SEL)selector
{
NSUInteger hash = NSUINTROTATE([clazz hash], NSUINT_BIT / 2) ^ (NSUInteger)selector;
return [NSNumber numberWithUnsignedInteger:hash];
}
SELs themselves are unique; you could use an NSValue and wrap just that:
[NSValue valueWithBytes:&selector objCType:#encode(SEL)];

NSNumberformatter quasi Scientific notations

I need the NSNumberFormatter to conditionally display very huge or very small numbers as Scientific notation. But if the number can be presented without, i need it to be only decimal.
6.62e-34 is Ok, but 4,2E1 is not.
Elsewhere in the system, I have solved this using the following method:
- (NSString *) formatNumber: (double) d {
BOOL sci=NO;
double dd = d;
if (dd<0.0) dd=(-d);
if ((dd>0.0) && (dd<1e-3)) sci=YES;
if (dd>1e+8) sci=YES;
if (sci) [nf setNumberStyle:NSNumberFormatterScientificStyle];
else [nf setNumberStyle:NSNumberFormatterDecimalStyle];
return [nf stringFromNumber:[NSNumber numberWithDouble:d]];
}
But at this particular place, the whole thing is set up in the interface builder, with bindings to an managed object modell and using an array controller object. In the first text cell of the colum in the tableview, I have dragged in the numberformatter. I dont know if its possible to define some custom methods to be called to do the formatting, instead of using this NumberFormatter. If I take it away, I got errors when I run it.
anyone have some idea ?
What you want to do is write a subclass of NSValueTransformer. You can then provide the name of that transformer class in IB, in the bindings inspector. Here is an example:
#implementation RDTransformer
- (id)init {
if (self = [super init]) {
self.formatter = [[NSNumberFormatter alloc] init];
[self.formatter setNumberStyle:NSNumberFormatterScientificStyle];
[self.formatter setMaximumFractionDigits:3];
}
return self;
}
+(Class)transformedValueClass {
return [NSString class];
}
+(BOOL)allowsReverseTransformation {
return NO;
}
-(id)transformedValue:(NSNumber *)value {
if ([value compare:#1000] == NSOrderedAscending) {
return value.stringValue;
}else{
return [self.formatter stringFromNumber:value];
}
}
In IB, the column (in a single column table) has its value bound to Array Controller.arrangedObjects with a value transformer of RDTransformer. I don't have any formatters connected to the table cells.

correct way to send messages to (id *) variables or with (id *) arguments

I have a Core Data validation method I wrote that will not compile. I can modify the method so it compiles... but then I get runtime errors. The two versions of the method are below (notice the missing '*' in the second version). This version compiles but gives the runtime error "+[NSCFNumber doubleValue]: unrecognized selector sent to class 0x7fff70a448e8":
- (BOOL)validateInitialValue:(id *)value error:(NSError **)error {
if ( *value == nil ) {
return YES;
}
if ( [*value doubleValue] < 0.0 ) {
return NO;
}
return YES;
}
This version gives compiler warnings and errors (see warnings and errors below):
- (BOOL)validateInitialValue:(id *)value error:(NSError **)error {
if ( value == nil ) {
return YES;
}
if ( [value doubleValue] < 0.0 ) {
return NO;
}
return YES;
}
compiler errors and warnings:
warning: Semantic Issue: Receiver type 'id *' is not 'id' or interface pointer, consider casting it to 'id'
warning: Semantic Issue: Method '-doubleValue' not found (return type defaults to 'id')
error: Semantic Issue: Invalid operands to binary expression ('id' and 'double')
I finally figured that the problem may be in the calling code:
- (void)setInitialValue:(NSNumber *)initialValue {
[self willChangeValueForKey:#"initialValue"];
[self validateValue:initialValue forKey:#"initialValue" error:nil];
[self setPrimitiveInitialValue:initialValue];
[self didChangeValueForKey:#"initialValue"];
}
I changed the caller to use an '&' before initialValue and used the first version of the method, and everything worked. So the new calling code has the one line changed to be this:
[self validateValue:&initialValue forKey:#"initialValue" error:nil];
But is it really necessary to have the '&'?? setPrimitiveInitialValue doesn't use the '&'. I feel like my understanding of Objective-C is just not developed enough yet and all you gurus out there will find this a trivial question with a very straight forward answer.
id itself represents a pointer. So when you use id * you are actually referring to a pointer-to-a-pointer. The first part of this excellent tutorial explains this concept.
Chances are, this is what you are looking for:
- (BOOL)validateInitialValue:(id)value error:(NSError **)error {
if ( value == nil ) {
return YES;
}
if ( [value doubleValue] < 0.0 ) {
return NO;
}
return YES;
}
You're right that the problem is the calling code. id * indicates a pointer to an id value. An object variable by itself is an id, so you want a pointer to that variable, which is what you get with the &.
The reason you pass a pointer is so that, if your validation method knows of a way to modify the value to make it valid, it can return YES and also return the valid object (by setting the variable). So, for example, if numbers less than 1 should be clamped to 0, you might do:
- (BOOL)validateInitialValue:(id *)value error:(NSError **)error {
if ( *value == nil ) {
return YES;
}
if ( [*value doubleValue] < 0.0 ) {
return NO;
}
if ( [*value doubleValue] > 0.0 && [*value doubleValue] < 1.0 ) {
*value = [NSNumber numberWithInt:0];
}
return YES;
}
setPrimitiveValue: doesn't need to set variables in the calling context, so it just takes an id. (Very few methods work like validateValue:forKey:error:. Generally, they'll do it that way if they want to return a BOOL to indicate whether they changed something, but they still need a way to return the changed value as well.)