nsstring

Do loops and convenience methods cause memory peaks with ARC?

…衆ロ難τιáo~ 提交于 2019-12-03 05:11:21
问题 I'm working with ARC and seeing some strange behavior when modifying strings in a loop. In my situation, I'm looping using NSXMLParser delegate callbacks, but I see the same exact behavior and symptoms using a demo project and sample code which simply modifies some NSString objects. You can download the demo project from GitHub, just uncomment one of the four method calls in the main view controller's viewDidLoad method to test the different behaviors. For simplicity's sake, here's a simple

How to check if an NSString contains one of the NSStrings in an NSArray?

南笙酒味 提交于 2019-12-03 05:06:57
I'm making an iOS app and I need to figure out if an NSString contains any of the NSStrings in an NSArray . BOOL found=NO; for (NSString *s in arrayOfStrings) { if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) { found = YES; break; } } Adam It may be a silly optimization for your use case, but depending on how large the array is that you are iterating, it may be helpful/more performant to use NSArray's indexOfObjectWithOptions:passingTest: method. With this method you pass some options and a block that contains your test. Passing the NSEnumerationConcurrent option will allow

iOS 组件化方案

拈花ヽ惹草 提交于 2019-12-03 04:51:06
一、基本概括 讲解 在组件化之前,app都是在一个工程里开发的,开发的人员也是比较少的,业务发展也不是非常快,项目中不引用组件化开发也是合适的。但是当开发人员越来越多,代码量也就越来越多,业务也就越来越复杂,这时候单一的开发模式会显露出一些弊端: 容易出现冲突(使用xib) 耦合较严重(代码没有明确的约束,代码臃肿) 开发效率不够高 为了解决这些问题,于是出现了组件化开发的策略,能带来好处如下: 加快编译速度 自由选择开发姿势(MVC/MVVM/FRP) 方便有针对性测试 提高开发效率 今天我们讲解一下组件化开发,也是自己项目中使用到的一种方式Target-Action方式。主要是基于Mediator模式和Target-Action,中间采用了Runtime来完成调用。这套组件化将远程和本地应用调用做了拆分,而且是本地调用是为远程调用提供服务。 下面是target-action的工作图: 调用方式: 本地组件调用: 本地组件A在一处调用 [[CTMediator sharedInstance] performTarget:targetName action:actionName params:@{...}] 向CTMediator发起了跨组件调用,CTMediator根据发送过来的target和action,然后经过OC的runtime机制转为target实例以及action

iOS - 小说阅读器分章节,支持正则分章节和按字数分章节

折月煮酒 提交于 2019-12-03 04:30:29
最近做了一个WIFI传书本地阅读功能,有所收获在这里记录下吧。 用户下载的书籍分为两种,一种是有章节格式的,比如 第一章,001章、等,这种可以用 正则 来直接分章节,还有绝大多数书籍是没有这种格式的,这种如果整本书来直接解析的话,对CPU要求比较大,可能会卡死闪退,所有手动分章节还是很有必要的,这种情况下我们采用按照两千字来分。 话不多说,开始吧。 1、WIFI传书把书传到APP沙盒里,这里我们采用的是 GCDWebServer ,很方便,这里就不做陈述了。 2、将沙盒里面的 .txt 文件转成 文本 ,这里的坑点也不少,我们专门写了一个 NSStringEncoding 解码的算法来转文字,可以解析多种编码方式的文本,这种算法只能适配iOS11及以上系统,其他系统只能采用系统UTF-8方法来解析,限制较多。 //转成文字 - (void)encodeWithURL:(NSString *)url result:(void (^)(NSString *content))result; - (void)encodeWithURL:(NSString *)url result:(void (^)(NSString *content))result { if (url.length == 0) { result(@""); return; } NSData *data =

Getting the substring from a certain character in NSString

纵然是瞬间 提交于 2019-12-03 04:19:20
If I have an NSString that is initially: "ABCDE*FGHI" How do I make it turn into "FGHI" In other words, everything from the asterisk onwards is kept. Likewise, how would I turn it into: "ABCDE" (everything up to the asterisk is kept) Thanks NSString *myString = @"ABCDE*FGHI"; NSArray *myArray = [myString componentsSeparatedByString:@"*"]; key 0 of myArray will contain @"ABCDE" key 1 will contain @"FGHI" If you want more than one character to be the separator, use componentsSeparatedByCharactersInSet : NSString *myString = @"ABCDE*FGHI-JKL"; NSArray *myArray = [myString

Returning an NSString from an NSError

天大地大妈咪最大 提交于 2019-12-03 04:12:24
I am using the NSURLRequest class in my iPhone app, and the method that calls it returns an NSString which is great for when the connection goes through ok, but the issue is I need to convert the NSError into an NSString so I can either return it back or run some if() statements on it. Any ideas? :) -[NSError localizedDescription] . (Also, every ObjC object inherited from NSObject implements -description which returns an NSString.) for folks new to objective c (me), following is example code that makes accepted answer from 'KennyTM' work -> [self showAlertWithTitle:@"Error:" withMessage:error

Converting an NSString* to char?

六眼飞鱼酱① 提交于 2019-12-03 04:10:01
问题 How can I convert a NSString* into a char? EDIT: Thanks to Vladimir for getting me there! NSString *myString = @"HelloWorld"; const char *stringAsChar = [myString cStringUsingEncoding:[NSString defaultCStringEncoding]]; Hope this helps someone in the future! 回答1: Simple to transfer const char *cfilename=[stringobject UTF8String]; 来源: https://stackoverflow.com/questions/2996657/converting-an-nsstring-to-char

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

假如想象 提交于 2019-12-03 03:57:16
Incorrect decrement of the reference count of an object that is not owned at this point by the caller on iPhone. It is happening with NSString which I clearly init and release within the for loop. I have tried to do the same as an autoreleases string but I get leaks. I assume the culprit is the stringbytrimming call. Any suggestions, by the way this does not leak, but I get the warning in build and analyze. Everything also works fine and the app does not crash. for(int i=0;i<storyQuantity;i++) { NSString *imageString = [[NSString alloc] init]; imageString = [[[storiesArray objectAtIndex:i]

“Whole word” search in a NSString through NSPredicate

孤街醉人 提交于 2019-12-03 03:51:56
I would like to search if in the attribute description (an NSString instance) there is a given word. I tried with this predicate: [NSPredicate predicateWithFormat:@"description CONTAINS[cd] %@", theWord]; It works, but it finds also sub-words . For example, given theWord : Car it will mach also this description : A Christmas Carol Instead, I would like that my predicate will match only a , or christmas , or carol . (I'm using Core Data and NSFetchRequest.) Something like this? NSString *matchStr = @".*\\bCarol\\b.*"; NSPredicate *pred =[NSPredicate predicateWithFormat: @"description MATCHES %@

Generate RSA Public Key from Modulus and Exponent

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm looking to generate a rsa public key (pem) from both the modulus and exponent in Objective-C. This function can be done in Java by the following; PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, exponent)); Although I'm having some trouble writing this for the iOS platform. Am aware of OpenSSL , but I couldn't find a successful method and am still unable to generate this key. Also took a look at SCZ-BasicEncodingRules-iOS but this returns a NSData value and I'm stuck trying to figure out