nsstring

iOS JavaScriptCore使用

浪子不回头ぞ 提交于 2019-12-06 03:43:42
JavaScriptCore是iOS7引入的新功能,JavaScriptCore可以理解为一个浏览器的运行内核,使用JavaScriptCore可以使用native代码(这里主要指objectiveC和swift)与js代码进行相互的调用,本文主要从几个方面进行了解。 native调用js代码 js调用native代码 异常处理 JavaScriptCore和webView的结合使用 要使用JavaScriptCore,首先我们需要引入它的头文档 ` #import <JavaScriptCore/JavaScriptCore.h> ` 这个头里面引入了几个重要的对象 #import "JSContext.h" #import "JSValue.h" #import "JSManagedValue.h" #import "JSVirtualMachine.h" #import "JSExport.h" JSContext是JavaScript的运行上下文,他主要作用是执行js代码和注册native方法接口 JSValue是JSContext执行后的返回结果,他可以是任何js类型(比如基本数据类型和函数类型,对象类型等),并且都有对象的方法转换为native对象。 JSManagedValue是JSValue的封装,用它可以解决js和原声代码之间循环引用的问题

Check string containing URL for “http://”

点点圈 提交于 2019-12-06 03:39:49
问题 I am trying to check the URL entered by the user, but I am fighting against some errors and warnings. -(BOOL) textFieldShouldReturn:(UITextField *)textField { //check "http://" NSString *check = textField.text; NSString *searchString = @"http://"; NSRange resultRange = [check rangeWithString:searchString]; BOOL result = resultRange.location != NSNotFound; if (result) { NSURL *urlAddress = [NSURL URLWithString: textField.text]; } else { NSString *good = [NSString stringWithFormat:@"http://%@",

NSData to NString conversion problem

时光总嘲笑我的痴心妄想 提交于 2019-12-06 03:32:39
问题 I'm getting an HTML file as NSData and need to extract some parts of it. For that I need to convert it to NSString with UTF8 encoding. The thing is that this conversion fails, probably because the NSData contains bytes that are invalid for UTF8. I have tried to get the byte array of the data and go over it, but each time I come across non ASCII character (hebrew letters for example) I get jibrish. Help will be appreciated. UPDATE: To Gordon - the NSData generated like that: NSData *theData =

iOS 不要用宏来定义你的常量

巧了我就是萌 提交于 2019-12-06 03:23:38
首先,预处理命令他不是一个常量!!!! 我们来看一段代码 #define avatar @"60" if (false) { #define avatar @"80" } NSLog(avatar); 这段代码会输出多少,我们将“avatar”定义为了60,然后在一个永远不会执行的代码里面重新定义了“avatar”为80,if语句中的代码 永远不会执行,但是在编译时期,编译器会编译这段代码,而这个时候编译器就会将avatar这个名字替换为@“80”,所以这段代码最后的输出结果就是 80。 当然这个时候编译器是会有一个警告的,但是不知道有多少同学会忽略这个警告。或者你会告诉我你对警告十分敏感,不会放过他的,但是记住你不是一个人在写代码,可能在别人的页面他给你重新定义了你的define,给你挖了一个大坑,还找不着……… 用const来定义一个常量 const修饰符定义的变量是不可变的,比如说你需要定义一个动画时间的常量,你可以这么做: static const NSTimeInterval kAnimateDuration = 0.3; 当你试图去修改“ kAnimateDuration”的值的时候,编译器会报错。更加重要的是用这种方法定义的常量是带有类型信息的,而这点则是define不具备的。 也许你已经发现了,如果你像这样定义: static const NSString *

Objc EXC_BAD_ACCESS when setting a NSString equal to another

独自空忆成欢 提交于 2019-12-06 03:21:09
I'm having some really REALLY weird issues with NSString. When I read from an input stream and convert the data to a string I'm not able to set anything equal to that string. Here's the code: NSString *name = r.URL.lastPathComponent; NSString *data; NSInputStream *stream = r.HTTPBodyStream; uint8_t byteBuffer[1]; [stream open]; if (stream) { // Get the request body from the stream. Used for setting the file name if (stream.hasBytesAvailable) { NSInteger bytesRead = [stream read:byteBuffer maxLength:4096]; NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:bytesRead encoding

String to float in objective c

穿精又带淫゛_ 提交于 2019-12-06 03:04:17
问题 I have a parser returning some string value I'd like to use as parameter for my class instance initialisation. I have a method asking two NSString and a float value, but I can't convert the string to float, here is my code: NSString *from = [[NSString alloc] initWithString:@"EUR"]; NSString *to = [[NSString alloc] initWithString:[attributeDict objectForKey:@"currency"]]; NSNumber *rate = [[NSNumber alloc] initWithFloat:[[attributeDict objectForKey:@"rate"] doubleValue]]; currency = [[Currency

iOS 加密与解密

感情迁移 提交于 2019-12-06 02:21:25
iOS RSA的网络安全模型、iOS签名机制总结(登录、token安全、签名) 一.登录、登录保持(http请求) 登录机制大概可以分为一下三个阶段: 1. 登录验证:是指客户端提供用户名和密码,向服务器提出登录请求,服务器判断客户端是否可以登录并向客户端确认。 2. 登录保持:是指客户端登录后, 服务器能够分辨出已登录的客户端,并为其持续提供登录权限的服务器。 3. 登出:是指客户端主动退出登录状态。 第一种网络请求情况(安全级别:II) 一般的情况是这个样子的:一但用户登陆成功(单方面MD5加密:服务器加密则客户端不加密,客户端加密则明文传输),服务器为客户端分配 sessionID(也可以称为userID),当然有些服务器不但为客户端分配了userID还有可能会为用户提供token了(这个下面会做解释), 然后每次网络请求都将sessionID当做参数传递给服务器。 优点 能够保持用户登录状态、区分用户,相对于不返回任何信息的登录要安全了一些。 缺点 如果通过网络嗅探器(例如:青花瓷)可以获取到http链接,这样子服务器返回的sessionID便会被获取到,这样子依然会造成信息泄露,并且还能被伪造请求(浏览器请求)。 第二种网络请求情况 (安全级别:III) 第一种存在明显的安全隐患,但是目前市面上的好多app依然采用第一种方法去实现登录、网络请求

Iphone iterate over substring occurrences of a NSString

喜你入骨 提交于 2019-12-06 02:17:00
问题 I would like to find all occurrences of a substring in a NSString, and iterate one by one to do some changes to that NSString. How should I do it? 回答1: How about // find first occurrence of search string in source string NSRange range = [sourceString rangeOfString:@"searchString"]; while(range.location != NSNotFound) { // build a new string with your changed values range = [sourceString rangeOfString:@"searchString" options:0 range:NSMakeRange(range.location + 1, [sourceString length] - range

How to truncate an NSString based on the graphical width?

六月ゝ 毕业季﹏ 提交于 2019-12-06 01:38:46
问题 In UILabel there's functionality to truncate labels using different truncation techniques (UILineBreakMode). In NSString UIKit Additions there is a similar functionality for drawing strings. However, I found no way to access the actual truncated string. Is there any other way to get a truncated string based on the (graphical) width for a given font? I'd like to have a category on NSString with this method: -(NSString*)stringByTruncatingStringWithFont:(UIFont *)font forWidth:(CGFloat)width

What locale argument to pass to NSDecimalNumber +decimalNumberWithString:locale: so it always works with NSString's using the dot (.) decimal mark?

烈酒焚心 提交于 2019-12-06 01:26:59
问题 I have an NSString which I want to convert into an NSDecimalNumber . The string is received from a server and is always formatted using the en_US locale like XXX.YYY and not like XXX,YYY . I want to create an NSDecimalNumber which accepts XXX.YYY regardless of the locale the user. The number is never displayed to the user, it's used to do internal math. Normally you'd do something like this: NSDecimalNumber *n = [NSDecimalNumber decimalNumberWithString:@"1.234"]; However, if the user is