Line Counting Function no longer works in Swift 3.0 - uilabel

I had a function that I was using in Swift 2.2 that I never had any problems with, now I'm getting an error when I try to use it.
The code is to change the number of lines a UILabel has to accommodate the size of the string.
func countLabelLines(label: UILabel) -> Int {
if let text = label.text {
let myText = text as NSString
let attributes = [NSFontAttributeName: label.font]
let width = label.bounds.width
let height = CGFloat.greatestFiniteMagnitude
let size = CGSize(width: width, height: height)
let labelSize = myText.boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil) //Code errors here
let lines = ceil(CGFloat(labelSize.height) / label.font.lineHeight)
return Int(lines)
}
return 0
}
And this is my error message:
> 2016-10-14 09:47:37.213 EZ Lists[23136:1997134] -[_SwiftValue pointSize]: unrecognized selector sent to instance 0x608000247380
2016-10-14 09:47:37.218 EZ Lists[23136:1997134] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue pointSize]: unrecognized selector sent to instance 0x608000247380'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e2ef34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010d93321e objc_exception_throw + 48
2 CoreFoundation 0x000000010e35ef34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010e274c15 ___forwarding___ + 1013
4 CoreFoundation 0x000000010e274798 _CF_forwarding_prep_0 + 120
5 UIFoundation 0x000000011362a2ba __NSStringDrawingEngine + 3204
6 UIFoundation 0x0000000113629610 -[NSString(NSExtendedStringDrawing) boundingRectWithSize:options:attributes:context:] + 202
7 EZ Lists 0x000000010d2beaf2 _TZFC8EZ_Lists11CustomFuncs15countLabelLinesfT5labelCSo7UILabel_Si + 1218
8 EZ Lists 0x000000010d2be0f2 _TZFC8EZ_Lists11CustomFuncs21fitLabelTextForHeightfT5labelCSo7UILabel10constraintCSo18NSLayoutConstraint11extraHeightV12CoreGraphics7CGFloat_T_ + 82
9 EZ Lists 0x000000010d2ce445 _TFC8EZ_Lists9AddItemVC21viewDidLayoutSubviewsfT_T_ + 309
10 EZ Lists 0x000000010d2ce472 _TToFC8EZ_Lists9AddItemVC21viewDidLayoutSubviewsfT_T_ + 34
11 UIKit 0x000000010eee73a8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1337
12 QuartzCore 0x000000011478dcdc -[CALayer layoutSublayers] + 146
13 QuartzCore 0x00000001147817a0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 QuartzCore 0x000000011478161e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x000000011470f62c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
16 QuartzCore 0x000000011473c713 _ZN2CA11Transaction6commitEv + 475
17 UIKit 0x000000010ee1c067 _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
18 UIKit 0x000000010f62bb30 __handleEventQueue + 5672
19 CoreFoundation 0x000000010e294311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
20 CoreFoundation 0x000000010e27959c __CFRunLoopDoSources0 + 556
21 CoreFoundation 0x000000010e278a86 __CFRunLoopRun + 918
22 CoreFoundation 0x000000010e278494 CFRunLoopRunSpecific + 420
23 GraphicsServices 0x0000000113f9da6f GSEventRunModal + 161
24 UIKit 0x000000010ee22f34 UIApplicationMain + 159
25 EZ Lists 0x000000010d2caa4f main + 111
26 libdyld.dylib 0x00000001120ce68d start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I'm still pretty new at Xcode / Swift and I understand that one of my Arguments isn't right, but I don't understand which is the problem. After first I was thinking it was with the CGSize, but I broke that out into it's own line and that doesn't have an issue.

It's the font. The problem is this line:
let attributes = [NSFontAttributeName: label.font]
Try writing this:
let attributes = [NSFontAttributeName: label.font!]
I think the exclamation mark should fix it.

Related

UICollectionView with CSStickyHeaderFlowLayout crashes on scroll up and down

I created a UICollectionView using CSStickyHeaderFlowLayout, because i wanted to have separate Header for UICollectionView and its sections. It is showing data successfully, but when i scroll up or down and as soon as it touches to top or bottom boundary it crashes, giving following error, I simply want separate Headers for collection view and its sections like we have in UITableView. If someone can help resolve this issue or point some alternative.
2016-12-06 11:50:51.881 TestStickyHeader[899:18365] * Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.5.2/UICollectionViewData.m:445
2016-12-06 11:50:51.929 TestStickyHeader[899:18365] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'layout attributes for supplementary item at index path ( {length = 2, path = 9 - 0}) changed from indexPath: {9, 0} zIndex: 1024 valid: YES kind: UICollectionElementKindSectionHeader to indexPath: {9, 0} zIndex: 1024 valid: YES kind: UICollectionElementKindSectionHeader without invalidating the layout'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104cbb34b exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010471c21e objc_exception_throw + 48
2 CoreFoundation 0x0000000104cbf442 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00000001042b2e4d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x00000001063f5c3b __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 1436
5 UIKit 0x00000001063f5093 -[UICollectionViewData validateLayoutInRect:] + 2984
6 UIKit 0x00000001063f835e -[UICollectionViewData layoutAttributesForCellsInRect:validateLayout:] + 170
7 UIKit 0x0000000106392909 -[UICollectionView _computePrefetchCandidatesForVisibleBounds:futureVisibleBounds:prefetchVector:notifyDelegateIfNeeded:] + 134
8 UIKit 0x0000000106392877 -[UICollectionView _computePrefetchCandidatesForVelocity:notifyDelegateIfNeeded:] + 214
9 UIKit 0x000000010639278d -[UICollectionView _prefetchItemsForVelocity:maxItemsToPrefetch:invalidateCandidatesOnDirectionChanges:] + 934
10 UIKit 0x000000010639baa7 -[UICollectionView layoutSubviews] + 717
11 UIKit 0x0000000105b12f50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
12 QuartzCore 0x00000001058c5cc4 -[CALayer layoutSublayers] + 146
13 QuartzCore 0x00000001058b9788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 QuartzCore 0x00000001058b9606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x0000000105847680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
16 QuartzCore 0x0000000105874767 _ZN2CA11Transaction6commitEv + 475
17 QuartzCore 0x00000001057cfb97 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 611
18 CoreFoundation 0x0000000104c4d964 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 20
19 CoreFoundation 0x0000000104c4d5f3 __CFRunLoopDoTimer + 1075
20 CoreFoundation 0x0000000104c4d17a __CFRunLoopDoTimers + 250
21 CoreFoundation 0x0000000104c44f01 __CFRunLoopRun + 2065
22 CoreFoundation 0x0000000104c44494 CFRunLoopRunSpecific + 420
23 GraphicsServices 0x0000000109e38a6f GSEventRunModal + 161
24 UIKit 0x0000000105a4e964 UIApplicationMain + 159
25 TestStickyHeader 0x0000000103d9411f main + 111
26 libdyld.dylib 0x000000010b44c68d start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Change Picker Rows after window is loaded

I'm trying to change the picker rows after the window was loaded. The picker was created in the xml file but when a row was attempted to be added to it an error is thrown (see below).
How do i add the picker rows once the picker is already added?
view.xml
<!-- other parent views above -->
<Picker id="languagePicker" selectionIndicator="true" useSpinner="true"
width="Ti.Platform.displayCaps.platformWidth" right="0">
</Picker>
view.js
var languages = db.execute("SELECT * FROM language");
while(languages.isValidRow()){
$.languagePicker.add(Ti.UI.createPickerRow({title:languages.fieldByName('language')}));
languages.next();
}
Error Log:
[ERROR] The application has crashed with an uncaught exception 'NSInvalidArgumentException'.
Reason:
-[NSNull rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x109f28af0
Stack trace:
0 CoreFoundation 0x0000000109d03e4d exceptionPreprocess + 141
1 libobjc.A.dylib 0x00000001096d5deb objc_exception_throw + 48
2 CoreFoundation 0x0000000109d0c48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000109c5990a ___forwarding_ + 970
4 CoreFoundation 0x0000000109c594b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000106466d9c -[UILabel _contentInsetsFromFonts] + 137
6 UIKit 0x0000000106782a84 -[_UILabelLayer updateContentInsets] + 127
7 UIKit 0x0000000106782b73 -[_UILabelLayer updateContentLayerSize] + 50
8 UIKit 0x0000000106782ce1 -[_UILabelLayer layoutSublayers] + 25
9 QuartzCore 0x000000010875fe70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
10 QuartzCore 0x000000010875fcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
11 QuartzCore 0x0000000108754475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
12 QuartzCore 0x0000000108781c0a _ZN2CA11Transaction6commitEv + 486
13 QuartzCore 0x000000010878237c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
14 CoreFoundation 0x0000000109c2f367 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23
15 CoreFoundation 0x0000000109c2f2d7 __CFRunLoopDoObservers + 391
16 CoreFoundation 0x0000000109c24f2b __CFRunLoopRun + 1147
17 CoreFoundation 0x0000000109c24828 CFRunLoopRunSpecific + 488
18 GraphicsServices 0x000000010b04bad2 GSEventRunModal + 161
19 UIKit 0x00000001061f9610 UIApplicationMain + 171
20 ISTQB 0x0000000104320de6 main + 310
21 libdyld.dylib 0x000000010a95192d start + 1
The picker.add() method expects an array of PickerRow objects. This works in my app:
var data = [],
picker = Ti.UI.createPicker();
data.push(
Ti.UI.createPickerRow({
title: 'Row 1'
}));
data.push(
Ti.UI.createPickerRow({
title: 'Row2'
}));
picker.add(data);
See http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.Picker-method-add

int value not passing, unrecognized selector sent to instance Terminating app due to uncaught exception 'NSInvalidArgumentException'

I'm trying to pass two integer values between different files but getting a error as mentioned in title.
Error at this line: - (NSString *) lookup: (NSString *)in get:(int)dictio get1:(int)dictio1;
Error File:
2014-02-05 13:27:48.019 MaisOui[4556:70b] Value of d: 10 d1: 0
2014-02-05 13:27:49.496 MaisOui[4556:70b] Value of d: 10 d1: 10
2014-02-05 13:27:56.405 MaisOui[4556:70b] -[MaisOuiDictionary lookup:]: unrecognized selector sent to instance 0x8c44d30
2014-02-05 13:27:56.409 MaisOui[4556:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MaisOuiDictionary lookup:]: unrecognized selector sent to instance 0x8c44d30'
*** First throw call stack:
(
0 CoreFoundation 0x0173f5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014c28b6 objc_exception_throw + 44
2 CoreFoundation 0x017dc903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0172f90b ___forwarding___ + 1019
4 CoreFoundation 0x0172f4ee _CF_forwarding_prep_0 + 14
5 MaisOui 0x00003299 -[MaisOuiViewController textFieldShouldReturn:] + 313
6 UIKit 0x0091761f -[UITextField keyboardInput:shouldInsertText:isMarkedText:] + 357
7 UIKit 0x004054f3 -[UIKeyboardImpl callShouldInsertText:] + 271
8 UIKit 0x0040f7d0 -[UIKeyboardImpl addWordTerminator:afterSpace:elapsedTime:executionContext:] + 63
9 UIKit 0x0040f2ca -[UIKeyboardImpl completeAcceptCandidateBeforeAddingInput:executionContext:] + 1668
10 UIKit 0x0040eba6 __60-[UIKeyboardImpl addInputString:withFlags:executionContext:]_block_invoke + 53
11 UIKit 0x0093ee7e -[UIKeyboardTaskExecutionContext returnExecutionToParent] + 254
12 UIKit 0x0041244b -[UIKeyboardImpl acceptCurrentCandidateIfSelectedWithExecutionContext:] + 250
13 UIKit 0x0040eb69 -[UIKeyboardImpl addInputString:withFlags:executionContext:] + 451
14 UIKit 0x0040da2f -[UIKeyboardImpl handleStringInput:withFlags:executionContext:] + 248
15 UIKit 0x0041bc1f -[UIKeyboardImpl handleKeyWithString:forKeyEvent:executionContext:] + 633
16 UIKit 0x0041b766 -[UIKeyboardImpl handleKeyEvent:executionContext:] + 1808
17 UIKit 0x0041afbf __33-[UIKeyboardImpl handleKeyEvent:]_block_invoke + 51
18 UIKit 0x0093f3c8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 402
19 UIKit 0x0093fa6f -[UIKeyboardTaskQueue addTask:] + 144
20 UIKit 0x0041af84 -[UIKeyboardImpl handleKeyEvent:] + 227
21 UIKit 0x0024440b -[UIApplication _handleKeyUIEvent:] + 330
22 UIKit 0x0038afa4 -[UIResponder(Internal) _handleKeyUIEvent:] + 59
23 UIKit 0x0038afa4 -[UIResponder(Internal) _handleKeyUIEvent:] + 59
24 UIKit 0x0038afa4 -[UIResponder(Internal) _handleKeyUIEvent:] + 59
25 UIKit 0x0038afa4 -[UIResponder(Internal) _handleKeyUIEvent:] + 59
26 UIKit 0x002442ba -[UIApplication handleKeyUIEvent:] + 84
27 UIKit 0x0024425e -[UIApplication handleKeyHIDEvent:] + 458
28 UIKit 0x0022c07c _UIApplicationHandleEventQueue + 2954
29 CoreFoundation 0x016c883f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
30 CoreFoundation 0x016c81cb __CFRunLoopDoSources0 + 235
31 CoreFoundation 0x016e529e __CFRunLoopRun + 910
32 CoreFoundation 0x016e4ac3 CFRunLoopRunSpecific + 467
33 CoreFoundation 0x016e48db CFRunLoopRunInMode + 123
34 GraphicsServices 0x036e49e2 GSEventRunModal + 192
35 GraphicsServices 0x036e4809 GSEventRun + 104
36 UIKit 0x00230d3b UIApplicationMain + 1225
37 MaisOui 0x00002c5d main + 141
38 libdyld.dylib 0x01d7d70d start + 1
39 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
#import "MaisOuiAppDelegate.h"
int main(int argc, char * argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([MaisOuiAppDelegate class]));
}
}
From file to pass:
int d=0;
int d1=0;
File passed to:
#interface MaisOuiDictionary : NSObject
- (NSString *) lookup: (NSString *) in;
- (int) dic : (int) dictio;
- (int) dic1 : (int ) dictio1;
#end
- (NSString *) lookup: (NSString *)in get:(int)dictio get1:(int)dictio1;
{
// if(dictio==10 && dictio1==10)
{
NSString *rv = [self.wordsE_S objectForKey: in];
if ( !rv )
return #"*** no translation ***";
return rv;
}
// return #"**nothing**";
}
Can someone tell me where I wrong. I have tried changing all values to int * and NSInteger *, but still getting the same error.
You've implemented this method
- (NSString*)lookup:(NSString*)in get:(int)dictio get1:(int)diction;
...but it looks like you're trying to call this method (that has been defined in your interface)
- (NSString*)lookup:(NSString*)in;
Because the implementation of lookup: doesn't exist, you're getting an exception thrown. You need to either:
Declare lookup:get:get1: in your interface, and call that in textFieldShouldReturn: instead of lookup:
Implement lookup: in your implementation
It all boils down to you calling a method that doesn't exist. Clarification on method naming in Objective C can be found here: Method Syntax in Objective C

textViewDidChange: crashes in iOS 7

As we know the UITextView is more powerful in iOS 7, but here it performs more fragile. The following code works fine when inputting "rr" with Chinese input keyboard in iOS 6, but crashes in iOS 7.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tv = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
self.tv.backgroundColor = [UIColor yellowColor];
self.tv.textColor = [UIColor blackColor];
self.tv.editable = YES;
self.tv.delegate = self;
[self.view addSubview:self.tv];
}
-(void)textViewDidChange:(UITextView *)textView{
int maxLength = 2;
if (self.tv.text.length > maxLength) {
NSLog(#"self.tv.text :%#",self.tv.text);
NSLog(#"self.tv.text.length :%d",self.tv.text.length);
NSLog(#"maxLength :%d",maxLength);
self.tv.text = [self.tv.text substringToIndex:maxLength];
}
}
The log is as following:
2013-11-13 15:48:16.003 Test2[1388:70b] self.tv.text :r r
2013-11-13 15:48:16.004 Test2[1388:70b] self.tv.text.length :3
2013-11-13 15:48:16.005 Test2[1388:70b] maxLength :2
2013-11-13 15:48:16.032 Test2[1388:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'
*** First throw call stack:
(
0 CoreFoundation 0x017355e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014b88b6 objc_exception_throw + 44
2 CoreFoundation 0x017353bb +[NSException raise:format:] + 139
3 Foundation 0x010ebba1 -[NSMutableRLEArray replaceObjectsInRange:withObject:length:] + 136
4 Foundation 0x010ebb14 -[NSMutableRLEArray deleteObjectsInRange:] + 63
5 Foundation 0x010ea559 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 324
6 UIFoundation 0x02d9d9f4 __71-[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:]_block_invoke + 68
7 UIFoundation 0x02d9d92f -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 121
8 UIKit 0x00924d22 __53-[UITextInputController setMarkedText:selectedRange:]_block_invoke + 352
9 UIFoundation 0x02d9b491 -[NSTextStorage coordinateEditing:] + 48
10 UIKit 0x00924a38 -[UITextInputController setMarkedText:selectedRange:] + 249
11 UIKit 0x008fb3b1 -[UITextView setMarkedText:selectedRange:] + 63
12 UIKit 0x00644aa5 -[UIResponder(UITextInput_Internal) _setMarkedText:selectedRange:] + 101
13 UIKit 0x004031aa -[UIKeyboardImpl setMarkedText:selectedRange:inputString:searchString:] + 365
14 UIKit 0x00419737 -[TIKeyboardOperationSetMarkedText(UIKeyboardImpl) main] + 178
15 Foundation 0x0118da69 -[__NSOperationInternal _start:] + 671
16 Foundation 0x0110a798 -[NSOperation start] + 83
17 UIKit 0x0040e08a -[UIKeyboardImpl updateCandidateDisplayAsyncWithCandidateSet:documentOperation:] + 188
18 UIKit 0x00419041 -[TIKeyboardOperationSetCandidates(UIKeyboardImpl) main] + 112
19 Foundation 0x0118da69 -[__NSOperationInternal _start:] + 671
20 Foundation 0x0110a798 -[NSOperation start] + 83
21 UIKit 0x0040671d -[UIKeyboardImpl performOperations:] + 153
22 UIKit 0x00404f4e -[UIKeyboardImpl continueGenerateCandidatesAsynchronouslyWithOperations:] + 40
23 UIKit 0x00404c51 __87-[UIKeyboardImpl replyHandlerForGenerateCandidatesAsynchronouslyWithSelectedCandidate:]_block_invoke_2 + 46
24 UIKit 0x009381b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 402
25 UIKit 0x0093885f -[UIKeyboardTaskQueue addTask:] + 144
26 CoreFoundation 0x01729d1d __invoking___ + 29
27 CoreFoundation 0x01729c2a -[NSInvocation invoke] + 362
28 UIKit 0x008e63d2 -[_UIActionWhenIdle invoke] + 100
29 UIKit 0x008e64a6 __41-[_UIActionWhenIdle addObserverToRunLoop]_block_invoke + 36
30 CoreFoundation 0x0172924d _runLoopObserverWithBlockContext + 29
31 CoreFoundation 0x016fd53e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
32 CoreFoundation 0x016fd48f __CFRunLoopDoObservers + 399
33 CoreFoundation 0x016db3b4 __CFRunLoopRun + 1076
34 CoreFoundation 0x016dab33 CFRunLoopRunSpecific + 467
35 CoreFoundation 0x016da94b CFRunLoopRunInMode + 123
36 GraphicsServices 0x036d69d7 GSEventRunModal + 192
37 GraphicsServices 0x036d67fe GSEventRun + 104
38 UIKit 0x0022b94b UIApplicationMain + 1225
39 Test2 0x0000388d main + 141
40 libdyld.dylib 0x01d7370d start + 1
41 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
In addition, I test the following code, it also crashes after input "rr" with Chinese input keyboard.
-(void)textViewDidChange:(UITextView *)textView{
int maxLength = 2;
if (self.tv.text.length > maxLength) {
self.tv.text = #"c";
}
}
Any help will be appreciated!
What's happening is that you're typing what is referred to as multistage text input, i.e. the input has to be confirmed from the user before it's actually committed into the underlying text. You get the crash because the text is only kind-of inputted, and until the text has been committed, it could easily change to something else.
To detect this situation, you're looking for the markedTextRange property of the UITextView, which indicates if you're in this complex input mode.
If the property is non-nil, then you're in this special input mode, so you should guard your modification code with something like:
if (self.tv.markedTextRange == nil && self.tv.text.length > maxLength) {
// Perform change
}
Near as I can tell the crash is triggered by the special multistage text input mode, so you should avoid changing the text while this mode is active.

unrecognized selector sent to instance of custom class

Here is class header:
#interface ChallengeItem : NSObject
.......
- (NSString *) getAlertIntervalInString;
- (void) createReminder;
#end
some implementation in .m file:
- (NSString *) getAlertIntervalInString {
... do something
}
- (void)createReminder {
... do something
}
I call
for (int i = 0; i < [self count]; i++) {
ChallengeItem *currentItem = [self getChallengeAtIndex:i];
if (currentItem.isStarted) {
[currentItem createReminder];
}
}
and receive an unrecognized selector error, but when I change
[currentItem createReminder];
to
[currentItem getAlertIntervalInString];
error isn't appear.
What is the problem?
Here is the console log
Current language: auto; currently objective-c 2011-11-23 00:56:40.056
30 Day Challenge[379:12b03] -[ChallengeItem createReminder]:
unrecognized selector sent to instance 0x6079900 2011-11-23
00:56:40.147 30 Day Challenge[379:12b03] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[ChallengeItem createReminder]: unrecognized selector sent to
instance 0x6079900'
* Call stack at first throw:
(
0 CoreFoundation 0x00fb35a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01107313 objc_exception_throw + 44
2 CoreFoundation 0x00fb50bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f24966 ___forwarding___ + 966
4 CoreFoundation 0x00f24522 _CF_forwarding_prep_0 + 50
5 30 Day Challenge 0x00006950 -[ChallengeCollection setRemindersForStartedChallenges] + 144
6 30 Day Challenge 0x00002f27 -[_0_Day_ChallengeAppDelegate applicationDidEnterBackground:] + 151
7 UIKit 0x00209a40 -[UIApplication _handleApplicationSuspend:eventInfo:] + 607
8 UIKit 0x00213039 -[UIApplication handleEvent:withNewEvent:] + 4127
9 UIKit 0x0020aabf -[UIApplication sendEvent:] + 71
10 UIKit 0x0020ff2e _UIApplicationHandleEvent + 7576
11 GraphicsServices 0x01712992 PurpleEventCallback + 1550
12 CoreFoundation 0x00f94944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
13 CoreFoundation 0x00ef4cf7 __CFRunLoopDoSource1 + 215
14 CoreFoundation 0x00ef1f83 __CFRunLoopRun + 979
15 CoreFoundation 0x00ef1840 CFRunLoopRunSpecific + 208
16 CoreFoundation 0x00ef1761 CFRunLoopRunInMode + 97
17 GraphicsServices 0x017111c4 GSEventRunModal + 217
18 GraphicsServices 0x01711289 GSEventRun + 115
19 UIKit 0x00213c93 UIApplicationMain + 1160
20 30 Day Challenge 0x00002059 main + 121
21 30 Day Challenge 0x00001fd5 start + 53
22 ??? 0x00000001 0x0 + 1
)
terminate called throwing an exception(gdb)
I don't see any obvious problems with your code. To make sure getChallengeAtIndex is in fact returning ChallengeItem instances, you could put a breakpoint at that line and inspect the returned values.
After you install the new version of XCode, the problem will be gone.