nsstring

static NSStrings in Objective-C

和自甴很熟 提交于 2019-12-06 00:45:06
问题 I frequently see a code snippet like this in class instance methods: static NSString *myString = @"This is a string."; I can't seem to figure out why this works. Is this simply the objc equivalent of a #define that's limited to the method's scope? I (think) I understand the static nature of the variable, but more specifically about NSStrings, why isn't it being alloc'd, init'd? Thanks~ 回答1: For the part of NSString alloc, init: I think first, it can be thought as a convenience, but it is not

Append a newline to an NSString

删除回忆录丶 提交于 2019-12-06 00:39:27
问题 I have this: if (soapResults != nil) { soapResults = [soapResults stringByAppendingString:@"\n"]; } But I get a warning: Assignment from distinct Objective-C type on build. When I run it on device I get: Program received signal: "EXC_BAD_ACCESS". from gdb. Any ideas how to append a newline to an NSString without getting a warning or error? Any help appreciated // :) 回答1: An alternative is: [NSString stringWithFormat: @"%@\n", soapResults] even if the logic is the same! However, even in the

Get selection (highlighted text) string from NSTextView Objective-C

落花浮王杯 提交于 2019-12-05 23:56:46
How can I get the selected text's string from a NSTextView as an NSString ? Your help is greatly appreciated. Since NSTextView is a subclass of NSText, you can use NSText instance methods to figure out the selected string like so: NSString *selected = [[myTextView string] substringWithRange:[myTextView selectedRange]]; An NSText can have more than only one selection. Check it out with TextEditapp: select a string with the mouse while pressing CMD. So you can select as many strings as you want. Therefore I think, a more common solution is to use: NSArray *ranges = [myTextView selectedRanges];

iOS - runtime - getProperty - getIvar

拟墨画扇 提交于 2019-12-05 23:47:22
iOS 中的runtime可以获取一个类的所有属性,成员变量,方法,协议等 通过获取到方法名,项目中可以直接通过selector 去调用第三方的方法,实现我们直接调用第三方api无法实现的方法 //属性 +(void)getProperties:(NSString*)className { u_int count = 0; objc_property_t *properties = class_copyPropertyList(NSClassFromString(className), &count); for (int i = 0; i < count; i++) { const char *propertyName = property_getName(properties[i]); const char *attributes = property_getAttributes(properties[i]); NSString *str = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]; NSString *attributesStr = [NSString stringWithCString:attributes encoding:NSUTF8StringEncoding];

How to limit characters in UITextView iOS

守給你的承諾、 提交于 2019-12-05 22:46:16
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 some text" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } } here is

How to append the following characters ‡, †, * as superscript to NSString in iOS

元气小坏坏 提交于 2019-12-05 22:02:43
I need append the following characters ‡, †, * as superscript to NSString in iOS . Need your help. I use the following http://en.wikipedia.org/wiki/General_Punctuation_(Unicode_block) link but they are appending to NSString , But i want them as superscript Try to use this one. And you need to #import <CoreText/CTStringAttributes.h> . This code works only in iOS6 or later version. UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 200, 40)]; NSString *infoString=@"X2 and H20 A‡ B† C*"; NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString

Difference between “.text” and “setText:”? [duplicate]

江枫思渺然 提交于 2019-12-05 20:53:54
This question already has answers here : Closed 7 years ago . Possible Duplicate: Is there a difference between setting a property with the dot or the bracket syntax? If I define a label in the .h file and I want to change the text of it in the .m file, is there really a difference between using label.text = @"..." and [label setText:@"..."] ? They appear to do exactly the same thing, and if that's the case, then why the different ways to do it? dasblinkenlight There is no difference in the inner workings. The first way requires the type of label to be such that the compiler could verify the

iOS: Draw NSString and border on UIImage

我怕爱的太早我们不能终老 提交于 2019-12-05 20:40:27
I am wanting to draw an NSString and a border onto a UIImage that I already have. I found a method that will draw an NSString as a UIImage, but I need it to draw on an image that I provide. -(UIImage *)imageFromText:(NSString *)text { // set the font type and size UIFont *font = [UIFont systemFontOfSize:20.0]; CGSize size = [text sizeWithFont:font]; // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) if (UIGraphicsBeginImageContextWithOptions != NULL) UIGraphicsBeginImageContextWithOptions(size,NO,0.0); else // iOS is < 4.0 UIGraphicsBeginImageContext(size); //

How do I convert NSString to NSData?

烂漫一生 提交于 2019-12-05 20:30:30
问题 I have this line of code to convert NSString to NSData: NSData *data = [NSData dataWithBytes:[message UTF8String] length:[message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; How do I do this in Unicode instead of UTF8? My message may contain cyrillic characters or diacritical marks. 回答1: First off, you should use dataUsingEncoding: instead of going through UTF8String . You only use UTF8String when you need a C string in that encoding. Then, for “Unicode” (specifically, UTF-16), just

Decoding partial UTF-8 into NSString

空扰寡人 提交于 2019-12-05 20:21:43
While fetching a UTF-8 -encoded file over the network using the NSURLConnection class, there's a good chance the delegate's connection:didReceiveData: message will be sent with an NSData which truncates the UTF-8 file - because UTF-8 is a multi-byte encoding scheme, and a single character can be sent in two separate NSData In other words, if I join all the data I get from connection:didReceiveData: I will have a valid UTF-8 file, but each separate data is not valid UTF-8 (). I do not want to store all the downloaded file in memory. What I want is: given NSData , decode whatever you can into an