nsstring

How to count words within a text string?

非 Y 不嫁゛ 提交于 2019-12-18 13:48:24
问题 On iOS, how can I count words within a specific text string? 回答1: A more efficient method than splitting is to check the string character by character. int word_count(NSString* s) { CFCharacterSetRef alpha = CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric); CFStringInlineBuffer buf; CFIndex len = CFStringGetLength((CFStringRef)s); CFStringInitInlineBuffer((CFStringRef)s, &buf, CFRangeMake(0, len)); UniChar c; CFIndex i = 0; int word_count = 0; Boolean was_alpha = false, is_alpha;

ios开发 Socket通信

时光怂恿深爱的人放手 提交于 2019-12-18 13:20:21
ios开发 Socket通信 Socket描述了一个IP、端口对。它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息。所以,Socket一定包含了通信的双发,即客户端(Client)与服务端(server)。 1)服务端利用Socket监听端口; 2)客户端发起连接; 3)服务端返回信息,建立连接,开始通信; 4)客户端,服务端断开连接。 1套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。 应用层通过传输层进行数据通信时,TCP会遇到同时为多个应用程序进程提供并发服务的问题。多个TCP连接或多个应用程序进程可能需要通过同一个 TCP协议端口传输数据。为了 区别不同的应用程序进程和连接 ,许多计算机操作系统为应用程序与TCP/IP协议交互提供了套接字(Socket)接口。应 用层可以和传输层通过Socket接口,区分来自不同应用程序进程或网络连接的通信,实现数据传输的并发服务。 2 建立socket连接 建立Socket连接至少需要一对套接字,其中一个运行于客户端,称为ClientSocket,另一个运行于服务器端,称为ServerSocket。 套接字之间的连接过程分为三个步骤:服务器监听,客户端请求,连接确认。 服务器监听:服务器端套接字并不定位具体的客户端套接字

JSON和XML

孤街醉人 提交于 2019-12-18 12:40:16
n网络 JSON和XML niOS学院 n李明杰 nJSON n什么是JSON pJSON是一种轻量级的数据格式,一般用于数据交互 p服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) p nJSON的格式很像OC中的字典和数组 {"name" : "jack", "age" : 10} {"names" : ["jack", "rose", "jim"]} p标准JSON格式的注意点:key必须用双引号 p n要想从JSON中挖掘出具体数据,得对JSON进行解析 pJSON 转换为 OC数据类型 nJSON – OC 转换对照表 nJSON – OC 转换练习 nJSON解析方案 n在iOS中,JSON的常见解析方案有4种 p第三方框架:JSONKit、SBJson、TouchJSON(性能从左到右,越差) p苹果原生(自带):NSJSONSerialization(性能最好) p nNSJSONSerialization的常见方法 pJSON数据 à OC对象 + (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; pOC对象 à JSON数据 + (NSData *)dataWithJSONObject:(id

How to know the length of NSString that fits a UILabel with fixed size?

谁说胖子不能爱 提交于 2019-12-18 12:32:45
问题 I know NSString has methods that determine the frame size for it, using NSString UIKit Additions, sizeWithFont...... How about the other way around? I mean if I have a fixed frame size, how do I know how many characters or words for a NSString that can fit into it? If I know this, I can cut off the NSString easily. thanks 回答1: It might not be the most elegant solution, but you could do something like this: - (NSString *)string:(NSString *)sourceString reducedToWidth:(CGFloat)width withFont:

Why does NSString respond to appendString?

旧城冷巷雨未停 提交于 2019-12-18 12:16:33
问题 I was playing with the respondsToSelector method in Objective-C on MacOS-X 10.6.7 and Xcode 4.0.2, to identify if an object would respond to certain messages. According to the manuals, NSString should not respond to appendString: while NSMutableString should. Here's the piece of code which tests it: int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *myString = [[NSString alloc] init]; if ([myString respondsToSelector:@selector

How to check if UITextField's text is valid email?

有些话、适合烂在心里 提交于 2019-12-18 11:35:17
问题 I have a view controller with 3 UITextFields (username, email, and password). I need a method that checks first, if all fields have text in them, then check if the email's textfield is a valid email, perhaps by checking if it has an @ sign in it. Can anyone help with this? 回答1: This will check a UITextField for a proper email. Add this method to the textFields delegate then check if the characters it is about to change should be added or not. Return YES or NO depending on the text fields

objective-c get last 2 characters of a string?

人盡茶涼 提交于 2019-12-18 10:26:42
问题 Say I have a string: NSString *state = @"California, CA"; Can someone please tell me how to extract the last two characters from this string (@"CA" in this example) 回答1: NSString *code = [state substringFromIndex: [state length] - 2]; should do it 回答2: Swift 4: let code = (state as? NSString)?.substring(from: state.characters.count - 2) 来源: https://stackoverflow.com/questions/3483223/objective-c-get-last-2-characters-of-a-string

NSUrl from NSString returns null

时光怂恿深爱的人放手 提交于 2019-12-18 09:44:28
问题 I want to parse the location of my Documents Directory to NSUrl from NSString I have. when my console NSLog(stringURL) i get: "/var/mobile/Applications/2B35D06D-6E4A-40F2-833E-28463A946834/Library/Application Support/MyAppDirectory" as an output. but when I do NSURL* url = [NSURL URLWithString:stringUrl]; and NSLog(url) i get (null) as an output. WHat am I doing wrong here? 回答1: From the apple documentation "An NSURL object initialized with URLString. If the URL string was malformed or nil,

Memory leak in NSString stringWithUTF8String: with ARC enabled

◇◆丶佛笑我妖孽 提交于 2019-12-18 09:37:05
问题 In my application i have enabled the ARC. But in my application following lines gives me memory leaks according to instruments. It is in ios 7.0. -(id)init{ variables = [[NSMutableArray alloc] init]; // Leak events = [[NSMutableArray alloc] init]; //Leak return self; } Update But in my app if i do something like below it does not show me any leak. But i can't add items in to the variables. -(id)init{ variables = [[[NSMutableArray alloc] init] copy]; // No Leak events = [[[NSMutableArray alloc

NSString encoding returns nil on url content

 ̄綄美尐妖づ 提交于 2019-12-18 09:35:04
问题 I'm following an iOS Swift guide on Udemy and this is the first issue I cannot work around: I am supposed to see html etc printed to the console but instead I get null. This is the section: let url = NSURL(string: "https://google.com") let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in if error == nil { var urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding) print(urlContent) } } task.resume() If I print just the data then it gives me