I am creating an application using collectionView.
I would like to implement the function of removing long pressed cells when I press and hold cells, but when I press and hold cells for a long time,
libc++abi.dylib:terminating with uncaught exception of type NSException appears in the debug area.
When I looked into it, it looked like a connection problem, so I tried to reconnect the outlet connection in the collectionView, but it didn't work.
Is there something wrong with the code?
I would appreciate it if you could let me know.Thank you for your cooperation.
Didn't you see more messages on the debug console besides the ones listed in the title when you pressed long on the cell?
It's been a little longer, but even if you solve the error in the title, the next one will be, so please read it carefully until the end.
I ran your code on the actual machine at hand and pressed it for a long time to see this message.(By the way, the simulator showed almost the same message.)
This output contains very important information.If the application terminates abnormally during the test, please be careful to check all the messages that appear when you perform the cause operation.Also, if you are not able to solve the problem yourself, please include all the messages in the body of the question.
This time, we found suspicious parts everywhere, and it was always a reproduction error, so we knew the cause right away, but it is often difficult to solve without these messages.
The most important thing is here.
An unrecognized selector was sent to the instance.(Class is app
ViewController class, selector is
In other words, your
ViewController doesn't have a
cellLongPressed method (from an Objective-C point of view), which results in runtime errors.This can be caused by "connection problems", but this is only one of many possible causes. Have you searched the
unrecognized selector sent to instance section? Let's look it up.
The worst part is this line.
In Swift, remember that the syntax
Selector(("character constant") should not be used.
If you look at the Swift code from ancient times, Xcode sometimes recommends this way of writing, but please don't swallow it.Let's not look at such ancient codes.
The method declared in the header
funccellLongPressPressed(sender:UILongPressGestureRecognizer) is the Selector
cellLongPressWithSender: for Objective-C.The last colon is also required for some method names.
There is a syntax that converts "Swift Method Name" to "Objective-C Selector" because this conversion is not normally possible for people who do not know Objective-C from Swift.
#selector(cellLongPressed) part can be written in various ways, such as
#selector syntax allows Swift to perform a confusing conversion to an Objective-C selector, and if you do not have such a method, she will display a error when compiling.
In , when you rewrite above, an error message similar to this appears along with the Fix It Suggestion.
@objc is added to the previous method header.
Here it is.
In your code,
deleteItems(at:) to tell
collectionView(_:numberOfItemsInSection:) keeps returning
Therefore, iOS reports runtime errors such as
NSInternalInconsistencyException in the sense of "I deleted it, but it's not decreasing!"
If you call
deleteItems(at:), you will need to comment out
collectionView(_:numberOfItemsInSection:) until you also reduce the number of
Without it, if it doesn't work, there are other parts that are not well organized.It would be better to delete it and correct the "other part".Of course, if it doesn't matter if you delete it, just delete it.
is rewritten like this with the current Swift.
Although it is longer in terms of number of characters,
drand() eliminates the need to use functions for the C language that you do not understand.
cell.backgroundColor=.init(red:.random(in:0...1), green:.random(in:0...1), blue:.random(in:0...1), alpha:0.6) can be written.)
However, if you use random numbers every time in
collectionView(_:cellForItemAt:), the color will change randomly when scrolling or when you delete and redraw cells.
Prepare an instance variable representing the color of the cell and
Initialize it in
This is how the
collectionView(_:numberOfItemsInSection:) is rewritten.
deleteItems(at:) and the rest will be consistent, so you will be able to make sure that
delete with long press
© 2022 OneMinuteCode. All rights reserved.