nsstring

“NSString stringWithUTF8String:” is overly touchy

走远了吗. 提交于 2019-12-07 12:50:15
问题 I'm in the middle of doing some string manipulation using high-level Cocoa features like NSString and NSData as opposed to digging down to C-level things like working on arrays of char s. For the love of it, +[NSString stringWithUTF8String:] sometimes returns nil on a perfectly good string that was created with -[NSString UTF8String] in the first place. One would assume that this happens when the input is malformed. Here is an example of the input that fails, in hex: 55 6B 66 51 35 59 4A 5C

How to limit characters in UITextView iOS

你离开我真会死。 提交于 2019-12-07 12:19:04
问题 I have a UITextView and i would like to limit the number of characters a user can input. i have also tried to display a warning if the textfield is empty but to no luck. textview.h @property (nonatomic,strong) IBOutlet UITextView *textField; @property (nonatomic, retain) NSString *userText; textview.m - (IBAction)next:(id)sender { self.userText = self.textField.text; if ([self.textField.text length] == 0) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Enter

NSURLSessionDownloadTask的后台下载与断点续传

跟風遠走 提交于 2019-12-07 12:07:42
对于后台下载与断点续传相信大家肯定不会陌生,那么如果要同时实现这两种需求该怎么办呢? 使用NSURLSessionDataTask可以很轻松实现断点续传,可是有个致命的缺点就是无法进行后台下载,一点应用程序进入了后台,便会停止下载.所以无法满足我们的需求.而NSURLSessionDownloadTask是唯一可以实现后台下载的类,所以我们只能从这个类进行下手了 网上关于NSURLSessionDownloadTask的断点续传资料很多,但是很遗憾的是基本都是一模一样的CV大法.而且只有一个暂停按钮暂停后继续下载,而关于应用程序被关闭后的断点续传只字不提. 那么本篇我们就来谈谈关于应用程序随时可能被杀死的情况下,如何进行断点续传. 关于断点续传原理: 首先,如果想要进行断点续传,那么需要简单了解一下断点续传的工作机制,在HTTP请求头中,有一个Range的关键字,通过这个关键字可以告诉服务器返回哪些数据给我 比如: bytes=500-999 表示第500-第999字节 bytes=500- 表示从第500字节往后的所有字节 然后我们再根据服务器返回的数据,将得到的data数据拼接到文件后面,就可以实现断点续传了. 关于NSURLSessionDownloadTask基础 大家可以参考下这篇文章 http://my.oschina.net/iOSliuhui/blog/469276

NSString font size specific to frame width

无人久伴 提交于 2019-12-07 11:46:58
问题 I am using drawRect for a text display, calling NSString . I am trying to implement using sizeWithFont to auto resizing font (shrinking) with default font size of 17 and using a loop to reduce the font size by 1 if it does not fit the size of width. Can anyone help me how to implement this? Example would be nice right now I just have the font size set to 17.0 [[self.string displayName] drawAtPoint:CGPointMake(xcoord, ycoord) withFont:[UIFont boldSystemFontOfSize:17.0]]; CGSize size = [[self

NSString stringWithFormat is slow

℡╲_俬逩灬. 提交于 2019-12-07 11:03:37
问题 In Objective-C, the method stringWithFormat: seems to be extremely slow and is actually a large bottleneck in one of our apps (we used the profiler to find that out). Is there a way to optimise it or use some faster C code? 回答1: Yes use sprintf in c http://www.cplusplus.com/reference/cstdio/sprintf/ after that push the char* in a NSString with [NSString stringWithUTF8:]; example: char cString[255]; sprintf (cString, "%d", 36); NSString* OCstring = [[NSString alloc] initWithUTF8String:cString]

How do I calculate the exact height of text using UIKit?

拟墨画扇 提交于 2019-12-07 10:29:51
问题 I'm using -[NSString sizeWithFont] to get the text height. The character 't' is clearly taller than 'm', but -sizeWithFont returns the same height for both these strings. Here's the code: UIFont* myFont = [UIFont fontWithName:@"Helvetica" size:1000.0]; NSString* myStr = @"m"; CGSize mySize = [myStr sizeWithFont:myFont]; With 'm' as shown, it returns {834, 1151} . With myStr = @"t" instead, it's {278, 1151} . The smaller width shows up as expected, but not the height. Does some other function

Get Unicode point of NSString and put that into another NSString

老子叫甜甜 提交于 2019-12-07 10:29:17
问题 What's the easiest way to get the Unicode value from an NSString ? For example, NSString *str = "A"; NSString *hex; Now, I want to set the value of hex to the Unicode value of str (i.e. 0041)... How would I go about doing that? 回答1: The unichar type is defined to be a 16-bit unicode value (eg, as indirectly documented in the description of the %C specifier), and you can get a unichar from a given position in an NSString using characterAtIndex:, or use getCharacters:range: if you want to fill

AFN传文件和参数

…衆ロ難τιáo~ 提交于 2019-12-07 10:09:51
使用AFNetWorking传文件和参数: #pragma mark - 上传问题图片 - ( void )uploadQuestionImage:( UIImage *)questionImage withQuestionID:( NSString *)questionID { AppDelegate * app = [ UIApplication sharedApplication ]. delegate ; NSData *imageData = UIImageJPEGRepresentation ( self . questionImage , 1.0 ); NSMutableDictionary * parameters = [ NSMutableDictionary dictionary ]; [parameters setObject :questionID forKey : @"exID" ]; NSMutableURLRequest *request = [[ AFHTTPClient clientWithBaseURL :[ NSURL URLWithString : dUrl_serverAddress ]] multipartFormRequestWithMethod : @"POST" path : @"Path/UploadTitleImage/"

convert base64 decoded NSData to NSString

和自甴很熟 提交于 2019-12-07 08:57:16
问题 I'm trying to encode and decode base64 data. but while decoding the base64 data, it returns bunch of hex values, but i couldn't display or printout using NSlog to the original readable strings. The below code couldn't print anything, just empty. Can anyone help ? thanks > > NSString* msgEncoded = [[NSString alloc] initWithFormat:@"Q1NNKE1DTC9TTUEgUkNWL2FkbWluQHNldGVjcy5jb20gT1JHLyBUVkIvNDNkYzNlMzQwYWQ3Yzkp:"]; NSData* decoded = [[NSData alloc] initWithData:[self decodeBase64WithString

使用 Clozure CL 在 Cocoa 窗口组件中实现显示中文字符串的简单方法

早过忘川 提交于 2019-12-07 07:51:10
使用 Clozure CL 在 Cocoa 窗口组件中实现显示中文字符串的简单方法 目录 0 概述 1 Objective-C 和 Lisp 的不同方法 2 英文标题代码示例 3 中文标题代码示例 0 概述 我们知道, 在 Cocoa 中提供了两种字符串类: NSString 和 NSMutableString, 前者用于处理内容不变, 固定的字符串, 比如窗口标题; 后者用于处理内容可变的字符串, 当然后者也可以用于前者的使用场合. 因为后者 NSMutableString 类是在继承 NSString 类的继承上创建的, 所以 NSMutableString 具有NSString 中所有的功能. 1 Objective-C 和 Lisp 的不同方法 在 Objective-C 代码中通常会使用如下的格式来定义字符串: NSString string = @"This is my string"; 语法非常简单明了, 但是有一个问题:使用加 @ 符号的方式只能定义英文数字字符串, 如果想定义中文等多字节字符串, 这种方法是不支持的. 下面给出一种 Objective-C 中定义中文字符串的示例: NSString* string; string = [NSString stringWithCString:"你好,我是中文字符串!" encoding