Question

I am using NSUserDefault (user defaults) to determine whether "Lists Sort Ascending" - the lists obtained from my Core Data stack using an NSFetchedResultsController - are sorted ascending or not.

For example:

NSSortDescriptor *sortDescriptorPrimary = [NSSortDescriptor sortDescriptorWithKey:self.sortAttributePrimary ascending:listsSortAscending selector:@selector(compare:)];

Regardless of the user setting, there are two Entities for which this value will always be true i.e. BOOL listsSortAscending = YES;, to be specific, alphabetically sorted lists, such as a contact list.

The code following this sentence is an attempt to explain what I am attempting to achieve...

BOOL listsSortAscending = YES;
if ((![self.entity isEqualToString:@"Contact"]) || (![self.entity isEqualToString:@"TypeChild"])){
    listsSortAscending = [self.userDefaults boolForKey:kListsSortAscending];
}

Apart from the lists for those two entities that will always be sorted ascending:YES, the other entities will be sorted based on the user default listsSortAscending.

This code does not work however and I cannot seem to work out the syntax correct.

I have read a lot of Stack Overflow and other pages including Objective-C IF statement with OR condition, however I cannot seem to resolve this annoying syntax!!!

The code included below does work, however my obsessive compulsive traits are running strong today and I want to learn the correct syntax using the OR operator ||.

BOOL listsSortAscending = YES;
if (![self.entity isEqual:@"Contact"]) {
    if (![self.entity isEqual:@"TypeChild"]) {
        listsSortAscending = [self.userDefaults boolForKey:kListsSortAscending];
    }
}

Any suggestions please?

Was it helpful?

Solution

I think you want:

if ((![self.entity isEqualToString:@"Contact"] && ![self.entity isEqualToString:@"TypeChild"])) {

It isn't really a syntax thing, its a negation thing. Sometimes it's easier to specify the positive case instead of the negative case. But your second section of code with the nested ifs is actually an && rather than an ||.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top