Wrong selection in ios 7 with same code - iphone

I am Using This code to select my default index title for table in my drop down.
When i run this code on ios 6 it enters in the else part and when i run it on ios 7 it enters in the if part whereas it should enter in the else part.
Please help me on this.
-(void)setSelectedIndexPath:(NSIndexPath *)selectedIndexPath
{
_selectedIndexPath = selectedIndexPath;
if (_selectedIndexPath.row == NSNotFound) {
[self.selectedValueLabel setText:self.title];
}
else{
[self.selectedValueLabel setText:[self.dataSource dropDown:self optionTitleForRowAtIndexPath:_selectedIndexPath]];
}
}

There are few changes in iOS 7 where nil is being used instead of a sentinel value. Set a break point and check if the indexPath is nil for some reason. If it is, [nil row] would return 0 and not NSNotFound.

Related

Fill and array and then remove its items

I receive the data from websocket in every second and I want to take 8 of them in an array and send to the different endpoint, and when the first 8 items is added, I want to remove all items inside the array and fill with the new ones, here is my codes:
if dataArray.count <= 7 {
dataArray.append(value)
if dataArray.count == 8 {
self.sendData(data: dataArray)
dataArray.removeAll()
}
}
Just want to know that there is a better / safer way to do that?
Thank you so much
Your code is fine, if you are worry about condition race, maybe consider below
Make a copy array before sending:
let valuesToSend = dataArray
Replace old one with new array once reach 8 items:
dataArray = [Data]()
I'm not sure if you need the first comparison.
dataArray.append(value)
if dataArray.count == 7 {
sendData(data: dataArray)
dataArray.removeAll()
}
And since you want the first 8 elements, shouldn't you compare the count with 8?
Also, you might want to check what happens in a possible overflow case (count > 8).
Another approach would be using a switch case
dataArray.append(value)
switch dataArray.count {
case 8:
sendData(data: dataArray)
dataArray.removeAll()
case 9...:
fatalError("Should never happen")
default:
}

UITest compare value of element in expectation

I am currently trying to figure out when an activity indicator is on or off. I realised that, when on, its value is 1, 0 when off.
I am trying to check if that value is 1 or 0 like this:
let app = XCUIApplication()
let predicate = NSPredicate(format: "value == 0")
let expectation = expectation(
for: predicate,
evaluatedWith: app.activityIndicators["activityId"],
handler: nil)
if XCTWaiter().wait(for:[expectation], timeout: 20) == .completed {
print ("Activity indicator is off")
} else {
print ("Activity indicator is on")
}
The current problem is that the timeout is always reached. Whenever I set a breakpoint at the if and type this in the console:
po app.activityIndicators["activityId"].value
The result is:
Optional<Any>
- some : 0
However, after I let the test run again, the timeout is reached and the else branch is taken.
Can anyone see what I am doing wrong while accessing the value of the element in the expectation?
Thanks for your help in advance.
Assuming you've both launched your app and set the accessibility identifier of your activity indicator correctly (which it seems you've done), the reason your code doesn't work is that value is a string, not an integer.
Replacing your predicate with the following will make your code work:
let predicate = NSPredicate(format: "value == \"0\"")

Issue with iPhone5 UICollectionView.visibleCells index

I'm having an issue with iPhone 5 specifically. In my UICollectionView's didSelectItemAt function, I have this snippet which iterates through all visible cells, and stops at the first cell which has a nil imageView.image.
for cell in heroPickerCollectionView.visibleCells {
let customIndex = heroPickerCollectionView.indexPath(for: cell)
let nilCell = heroPickerCollectionView.cellForItem(at: customIndex!) as! HeroPickerCell
if nilCell.imageView.image == nil {
selectedHeroArray.insert(heroData.heroes[(indexPath as NSIndexPath).row], at: (customIndex! as NSIndexPath).row)
nilCell.imageView.image = UIImage(named: heroData.heroes[(indexPath as NSIndexPath).row].imageName!)
checkForHeroes()
break
}
}
This works as intended on every device except iPhone 5. On iPhone 5, the customIndex always starts 0, 3 instead of 0, 0 (as it should be). Obviously this crashes because it attempts to insert at an index out of range.
I'm completely at a loss on this one. heroPickerCollectionView.visibleCells.count returns the correct count on iPhone 5, but the indexes are off.

Why Does String Not Equal What Is Stored?

This is a simple, odd question...
if(tableViewNum == #"One") {
if ([drinkArray objectAtIndex:0] == currentDate) {
[updatedArray addObject:drinkArray];
NSLog(#"MADE THE ELSE1");
}
NSLog(#"MADE THE ELSE2");
}
else if (tableViewNum == #"Two") {
if ([[drinkArray objectAtIndex:0] isEqualToString:yesterdayDate])
[updatedArray addObject:drinkArray];
} else {
NSLog(#"MADE THE ELSE %#",tableViewNum);
[updatedArray addObject:drinkArray];
}
In the very first if statement I ask if tableViewNum == #"One"
But I don't go in that section of the if statement even though tableViewNum actually does equal #"One"
As you can see the very last NSLog all ways comes out as
MADE THE ELSE One
But if tableViewNum really equaled One it would have gone through the if statement not the else statement code...?????
You can't compare strings with the == operator. Use isEqualToString instead:
if([tableViewNum isEqualToString:#"One"]) {
// etc.
… and the same for the rest of the conditions. You're already doing it right in the second block.
To be more specific, you shouldn't compare ANY objects using ==. This compares just the pointers. Use [obj isEqual: otherObj] or with NSStrings isEqualToString: as described above.

Validation, UITextField

I am working on some simple form validation and need some assistance. Basically, I just need to make sure a UITextField doesn't have a '0' or no value whatsoever (nil) when a user runs a simple calculation. If the field does contain either/or a label will be changed to notify the user. Here is my statement:
if ([abc.text isEqualToString:#"0"] || [time.text isEqualToString:nil]) {
self.123.text = #"Please enter a time";
} else { whatever }
Currently the 123 label is outputting NaN if nothing is entered into the abc UITextField.
if (![abc.text isEqualToString:#"0"] &&
![time.text == nil])
{
self.123.text = #"Please enter a time";
}
else
{
whatever
}
Replace [time.text isEqualToString:nil] with [time.text isEqualToString:#""]
You are trying to compare string with a nil object, and since a nil object (nil) is not the same as an empty string (#""), it fails.