nsstring

IOS(swift)-数据存储 · NSKeyedArchiver 归档

五迷三道 提交于 2019-12-06 07:00:28
需求: 有一个通讯列表,可以自行添加联系数据,但是重新开启后,添加的数据都会清空,我希望打开后,上一次的数据能保留。 这我们就必须用到数据持久化,这一次,我将用NSKeyedArchiver 归档 方式来做数据持久化,达到再次打开应用后,数据依然存在。 通讯录.png NSKeyedArchiver、NSKeyedUnarchiver ,主要用在ios数据存储上,数据从内存存储到闪存上,这个过程称为归档。Apple官方文档中,这些数据类型包括: NSData、NSString、NSNumber、NSDate、NSArray、NSDictionary。很显然,复杂数据例如UIImage,无法直接归档。但我们有一种变通的做法,先将UIImage对象转换为NSData,再对NSData进行归档。 一、创建一个数据模型(自定义类) import UIKitclass JKContactModel: NSObject,NSCoding{ var name:NSString! var phone:NSString! func encodeWithCoder(aCoder: NSCoder){ aCoder.encodeObject(self.name, forKey: "name") aCoder.encodeObject(self.phone, forKey: "phone") }

Parse Query String into a Structured NSDictionary

岁酱吖の 提交于 2019-12-06 06:58:41
I have a query string: a=1&b=2&c[1]=3&c[2]=4 etc… I want a NSDictionary where a => 1 , b => 2, c => [3,4] . Notice that that the value for c is an array. It should also be able to handle something like c[1][2]=5 to make an array of arrays c => [[5]] . Of course I can do it myself by splitting on the & and = , but what about other cases such as arrays and arrays of arrays. I want a structured NSDictionary from a POST request queryString and do not want to rewrite the wheel if this already exists. Are there any class/method, through Apple or 3rd party, that will parse a query string into a

How to gzip NSData with zlib?

只谈情不闲聊 提交于 2019-12-06 06:19:23
I want to use zlib because I'm assuming it's the best & fastest way to gzip NSData . But, is there a better way? If not, which version of the zlib library should I link to in Xcode: libz.dylib, libz.1.dylib, libz.1.1.3.dylib, or libz.1.2.5.dylib? Please provide a code example of how to us zlib to convert NSData *normalHTTPBody into NSData gzippedHTTPBody Yes, zlib is what is used to gzip data. I know of no better way. As for speed, you can select the compression level to optimize speed vs. compression for your application. You will likely find that libz.dylib and libz.1.dylib are symbolic

NSString or NSCFString in xcode?

雨燕双飞 提交于 2019-12-06 06:12:37
I m taking a NSMutabledictionary object in NSString like this : NSString *state=[d valueForKey:@"State"]; Now sometimes state may be null and sometimes filled with text.So Im comparing it.While comparing state becomes NSString sometimes and NSCFString othertimes..So unable to get the desired result.. if([state isEqualToString@""]) { //do something } else { //do something } So while comparing it is returning nil sometimes.So immediately jumping into the else block. I need a standard way to compare if the state is empty whether it is a NSString or NSCFString ... How can I do it? If you're unable

Convert array to NSAttributedString

狂风中的少年 提交于 2019-12-06 05:51:55
I have a NSMutableArray composed of NSAttributedString 's. I'm trying to convert it to a single NSAttributedString separating all the NSAttributedString 's with a character. This approach is similar to the conversion of an array to a NSString with the componentsJoinedByString:@"," method but unfortunately this method doesn't exist with the NSAttributedString . How can I convert the array? Thanks. I'd to the old way: NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init]; NSAttributedString *jointElement = [[NSAttributedString alloc] initWithString:@","] for (int i = 0; i

NSString drawInRect:withAttributes: not correctly centered when using NSKernAttributeName

白昼怎懂夜的黑 提交于 2019-12-06 05:48:21
问题 When I use drawInRect:withAttributes: and pass in both a paragraph style with NSTextAlignmentCenter and a non-zero value for NSKernAttributeName , the string does not get centered correctly. Am I doing something wrong or is this expected behavior? Is there a workaround? Screenshot: You can clearly see that top text is not centered correctly. My demo code: - (void)drawRect:(CGRect)rect { // Drawing code UIFont *font = [UIFont systemFontOfSize:15.0]; [self drawString:@"88" inRect:rect font:font

How to identify view with NSString tag?

我是研究僧i 提交于 2019-12-06 05:39:48
问题 view.tag is only stored NSInteger Value. so, how to identify each view with NSString Value? not avaliable? some example: view0.stringTag = @"basic"; view1.stringTag = @"advanced"; 回答1: There's no stringTag property on UIView . If you need to do this kind of thing you can use a category on UIView and store the tag in an associated object: @interface UIView (StringTagAdditions) @property (nonatomic, copy) NSString *stringTag; @end @implementation UIView (StringTagAdditions) static NSString

Phone number format ios

左心房为你撑大大i 提交于 2019-12-06 04:46:15
问题 In my app I am taking phone number as an input from user. Number should be in US format. I want to display it like (555)-888-888 dynamically. For example when user starts to input number when he reaches to 4 digit it shows number like this (555)-4 and so on. I tried to replaceString method but i found that it will not work. 回答1: Look at NBAsYouTypeFormatter class of libPhoneNumber-iOS library. You create new instance of NSAsYouTypeFormatter with your US region code given: NBAsYouTypeFormatter

Is it possible to insert Image between NSString?

折月煮酒 提交于 2019-12-06 04:43:13
问题 I am working on a product based application in which it shows product name with price increement or decreement. As of now, according to my requirement I am inserting a UIImage as a subview to UILabel.But its everytime I need to calculate product name length on which I am defining x position of UIImage and adding it again and again.Product name is of variable size. Not at all sometimes x position of image could not set properly so it overlaps text on UILabel.I stucked with this problem. Below

Trouble passing Swift 4 String to Obj-C++ -[_SwiftValue dataUsingEncoding:]: unrecognized selector sent to instance

為{幸葍}努か 提交于 2019-12-06 03:56:27
I have some Objective-C++ code like this: // header @interface MyObjcClass - (void) myMethod: (NSString *) text; @end // implementation @implementation MyObjcClass - (void) myMethod: (NSString *) text { someInternalObject->useWstring(nsStringToWstring(text)) } std::wstring nsStringToWstring(const NSString * text) { NSData * data = [text dataUsingEncoding: NSUTF32LittleEndianStringEncoding]; // and then some other stuff irrelevant to this question return std::wstring(pointerToNewString, pointerToNewString + stringLength); } @end Straightforward so far; seems fine to me. And works great when