nsrange

How do I use NSRange with this NSString?

自闭症网瘾萝莉.ら 提交于 2019-12-02 01:55:47
I have the following NSString : productID = @"com.sortitapps.themes.pink.book"; At the end, "book" can be anything.... "music", "movies", "games", etc. I need to find the third period after the word pink so I can replace that last "book" word with something else. How do I do this with NSRange? Basically I need this: partialID = @"com.sortitapps.themes.pink."; You can try a backward search for the dot and use the result to get the desired range: NSString *str = @"com.sortitapps.themes.pink.book"; NSUInteger dot = [str rangeOfString:@"." options:NSBackwardsSearch].location; NSString *newStr =

Prevent NSRangeException in subarrayWithRange

匆匆过客 提交于 2019-12-02 01:42:01
I have this code which allows me to pass in an index, and selectively retrieve, a number of images in an array for a certain range length - depending on orientation. When in portrait the range should be be 20 items per index, and i have 43 items altogether. However when i pass in the last index, i get an out of range exception for index 59 beyond bounds of [0..42]. NSArray *tempArray = [self imageData]; UIDeviceOrientation devOr = [[UIDevice currentDevice] orientation]; int kItemsPerView; if (UIDeviceOrientationIsPortrait(devOr)) { kItemsPerView = 20; }else { kItemsPerView = 14; } NSRange

How to print a NSRange in NSLog

被刻印的时光 ゝ 提交于 2019-12-01 13:44:43
问题 Say my range is created as NSRange myRange = {0,100}; How do I print myRange in NSLog? The following is not working NSLog(@"my range is %@",myRange); 回答1: Use NSStringFromRange . NSLog(@"my range is %@", NSStringFromRange(myRange)); In Swift you can do: print("my range is \(NSStringFromRange(myRange))") 回答2: Use NSStringFromRange(myRange). 来源: https://stackoverflow.com/questions/25212791/how-to-print-a-nsrange-in-nslog

UITextView firstRectForRange not working when there's new line characters in the mix

别来无恙 提交于 2019-12-01 10:54:22
I'm using this method for converting a NSRange to a CGRect as it relates to a UITextView: - (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView { UITextPosition *beginning = textView.beginningOfDocument; UITextPosition *start = [textView positionFromPosition:beginning offset:range.location]; UITextPosition *end = [textView positionFromPosition:beginning offset:range.location+range.length]; UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end]; CGRect rect = [textView firstRectForRange:textRange]; return rect; } This question 's answer has someone

Objective C: NSRange or similar with float?

蹲街弑〆低调 提交于 2019-12-01 06:17:51
For some methods I want them to return a range of values (from: 235, to: 245). I did this using NSRange as a return value - (NSRange)giveMeARangeForMyParameter:(NSString *)myParameter; This works fine as long as the return value is an integer range (e.g. location: 235, length: 10). But now I have the problem that I need to return a float range (e.g. location: 500.5, length: 0.4). I read in the doc that NSRange is a typedefed struct with NSUIntegers . Is it possible to typedef another struct for float ranges? And if so, can there be a similar method to NSMakeRange(NSUInteger loc, NSUInteger len

String, substring, Range, NSRange in Swift 4

99封情书 提交于 2019-12-01 02:44:26
I am using the following code to get a String substring from an NSRange : func substring(with nsrange: NSRange) -> String? { guard let range = Range.init(nsrange) else { return nil } let start = UTF16Index(range.lowerBound) let end = UTF16Index(range.upperBound) return String(utf16[start..<end]) } (via: https://mjtsai.com/blog/2016/12/19/nsregularexpression-and-swift/ ) When I compile with Swift 4 (Xcode 9b4), I get the following errors for the two lines that declare start and end : 'init' is unavailable 'init' was obsoleted in Swift 4.0 I am confused, since I am not using an init. How can I

Detect Character Tapped in UITextView

て烟熏妆下的殇ゞ 提交于 2019-11-30 16:26:48
I'm using the below code to detect words tapped in a UITextView. This works fine, but I want to detect some special characters, like a ? . ? doesn't show up as part of a word when using UITextGranularityWord and I can't seem to get it to show up when using UITextGranularityCharacter either. How can I detect taps on single special characters such as the ? ? -(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv { //eliminate scroll offset pos.y += _tv.contentOffset.y; //get location in text from textposition at point UITextPosition *tapPos = [_tv closestPositionToPoint:pos]; /

How shouldChangeCharactersInRange works in Swift?

倖福魔咒の 提交于 2019-11-29 21:17:55
I'm using shouldChangeCharactersInRange as a way of using on-the-fly type search. However I'm having a problem, shouldChangeCharactersInRange gets called before the text field actually updates: In Objective C, I solved this using using below: -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string]; return YES; } However, I've tried writing this in Swift: func textField(textField: UITextField!, shouldChangeCharactersInRange

Difference between NSRange and NSMakeRange

我的梦境 提交于 2019-11-29 13:07:46
Is there any difference between: NSRange(location: 0, length: 5) and: NSMakeRange(0, 5) Because Swiftlint throws a warning when I use NSMakeRange , but I don't know why. Thanks for the Help :-) The only difference between them is that NSRange(location: 0, length: 5) is an initializer for NSRange while NSMakeRange(0, 5) is a function which creates a new NSRange instance (by using the same initializer inside most likely) and actually is redundant in Swift . Swift has simply inherited it from Objective-C . I would stick to the former Main difference is that NSRange(location: 0, length: 24) is

Get currently typed word in a UITextView

血红的双手。 提交于 2019-11-29 11:36:46
I want to get the currently typed word in a UITextView . A way to get a completely typed word can be found here UITEXTVIEW: Get the recent word typed in uitextview , however, I want to get the word while it is being typed (no matter where it is typed, beginning, middle, end of UITextView ). I guess what defines a word being typed is a whitespace character followed by alphanumeric characters, or if the current location (somehow to figure out with range.location ) is the beginning of the UITextView then whatever follows up should be considered as word as well. A word is finished being typed when