GVKun编程网logo

NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换(nsnumber转nsstring)

5

关于NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL等等互相转换和nsnumber转nsstring的问题就给大家分享到这里,感谢你

关于NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换nsnumber转nsstring的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于20120810-课堂笔记--FoundationKit,NSRange,NSPoint,NSSize,NSRect,可变对象、cocoa-touch – NSDate作为NSDictionary的键、ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.、iOS NSString,NSArray,NSDictionary属性关键字copy等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换(nsnumber转nsstring)

NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换(nsnumber转nsstring)

一:Nsstring和NSURL 转换

 

 
  1. //Nsstring->NSURL  
  2. Nsstring *urlString=[@"http://www.google.com" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
  3. NSURL *url=[NSURL URLWithString:urlString];   
  4. //NSURL->Nsstring   
  5. Nsstring *urlString=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  

 

二:Nsstring和NSData转换(即可作json串互相转换)

 

 
  1. //NSData-> Nsstring  
  2. Nsstring *testString = [[Nsstring alloc] initWithData:testData encoding:NSUTF8StringEncoding];  
  3. //Nsstring->NSData  
  4. Nsstring *aString = @"我是Nsstring";  
  5. NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];  

 

三:NSData和NSImage转换

 

[cpp]  view plain copy print ?
 

20120810-课堂笔记--FoundationKit,NSRange,NSPoint,NSSize,NSRect,可变对象

20120810-课堂笔记--FoundationKit,NSRange,NSPoint,NSSize,NSRect,可变对象

Foundation 框架

 定义了一些基础类,可以用于各种典型的cocoa程序

层次的根是NSObject类,定义了基本的对象属性和行为

的剩余部分由几组


Application 框架


实现图形、事件驱动用户界面所需的对象:窗口,对话框,按键,菜单,滚动条,文本输入框等;

由超过125个类和协议组成。所有的类最终都从Foundation框架的NSObject 类继承而来。

2.NSRange

typedef struct _NSRange {

unsigned int location;

unsigned int length;

}NSRange;


struct _NSRange  = NSRange

含义:

一个结构体,表示相关事物的范围。

location 字段为该范围的起始位置;

length字段为该范围内所含元素个数。

创建方式:

直接给字段赋值

应用c语言的聚合结构赋值机制

使用cocoa提供的快捷函数NSMakeRange()

例子:

对字符串取子串

#import <Foundation/Foundation.h>

int main (int argc,const char * argv[])
{   
    //输出ipa
    Nsstring *homebrew = @"Imperial india pale ale(ipa)";
    
    NSRange range = NSMakeRange(25,3);
    //一下代码等价
   // NSRange range = {25,3};
    NSLog(@"beer shortname:%@",[homebrew substringWithRange:range]);
    return 0;
}

结果是 ipa;


3.NSPoint

初始化 NSPoint 使用函数NSMakePoint


NSPoint p = NSMakePoint(10,45);


从一个结构体变量获取坐标

float x = p.x;

float y = p.y;


4.NSSize


初始化NSSize


NSSize s = NSMakeSize(10,45)

从一个结构体变量获取width,heigth

float width = s.width;

float height = s.heigth;

5.NSRect

一个结构体,由点,大小组成而成;存储一个矩形的起始点和长度,宽度。

#import <Foundation/Foundation.h>

int main (int argc,const char * argv[])
{   
    NSRect r1 = NSMakeRect(0,100,120);
    
    NSRect r2 = {0,120};
    
    //对结构体最底层的成员赋值
    NSRect r3;
    r3.origin.x = 0.0;
    r3.origin.y = 0.0;
    r3.size.width = 100.0;
    r3.size.height = 120.0;

    
    return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Q:

常见的数据都是c的struct而不是oc对象?

因为性能,oc对象是通过动态分配,而动态分配是一个代价较高的操作,会消耗大量的时间。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


6.可变对象和不可变对象


创建一个oc对象后,该对象的内容不可更改,则称该对象为不可变对象。

仅可进行读操作,不能进行写操作;


该对象内容可以更改,则称为可变对象

即可进行读操作,也可进行写操作,还可改变大小。

7.Nsstring

Nsstring是不可变的,即一旦创建则不可改变,可对它执行各种读操作,但不能删除,添加字符,也不可改变内容。

Nsstring是一个对象类型,是NSObject的子类,具有NSObject的所有特性。

cocoa-touch – NSDate作为NSDictionary的键

cocoa-touch – NSDate作为NSDictionary的键

是否可以在NSDictionary中添加NSDate作为键和一些数组?

我没有要求将字典写入磁盘 – 归档或取消归档,就像这个人需要它一样:NSDictionary with NSDates as keys

但是,为什么我希望将NSDate作为键特别添加,以便我可以从字典中获取所有键并进行一些计算,例如从一个范围内获取日期.

具有相同日期值的两个对象在创建时是否共享相同的内存?

是否可以将NSDate作为关键?此外,我可能会面临这个假设的其他问题?

谢谢,

拉吉

编辑:
在发布我的问题后,我刚写了一个示例代码:

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateComps setDay:1];
[dateComps setMonth:1];
[dateComps setYear:2012];
NSDate *date1 = [[NSCalendar currentCalendar] dateFromComponents:dateComps];
NSDate *date2 = [[NSCalendar currentCalendar] dateFromComponents:dateComps];
NSLog(@"Date 1 = %x,Date 2 = %x",date1,date2);

输出:

Date 1 = 7945610,Date 2 = bd03610

但是,NSDictionary的关键比较是否将这两个NSDate对象解释为不同?

另一个编辑:

另外,如果我应该使用NSDateFormatter将NSDate转换为Nsstring,我无法直接检查范围内的日期.我将不得不执行以下步骤:

>从字典中获取所有Nsstring键
>将它们转换回NSDate数组
>执行谓词并获取范围内的日期
>再次将这些日期转换回字符串数组
>现在使用此字符串键从字典中获取值!

那么,还有更好的方法吗?

解决方法

是的,NSDate对象可以用作NSDictionary的键 – 只要它支持NScopying协议,任何对象类型都可以用作键.使用isEqual:而不是指针值来比较密钥.有关更多详细信息,请参阅NSDictionary文档的Overview部分.

ImportError: ERROR: recursion is detected during loading of

ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.

 问题:

pyinstaller 打包后,运行生成的exe报错 “recursion is detected during loading of “cv2“ binary extensions.”

 

Traceback (most recent call last):
  File "Sy.py", line 15, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "cv2\__init__.py", line 180, in <module>
    bootstrap()
  File "cv2\__init__.py", line 152, in bootstrap
    native_module = importlib.import_module("cv2")
  File "importlib\__init__.py", line 126, in import_module
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "cv2\__init__.py", line 180, in <module>
    bootstrap()
  File "cv2\__init__.py", line 75, in bootstrap
    raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.

 

========================

使用pip卸载opencv,pip uninstall opencv*******(具体的安装的opencv的名字)
到%:\python\Lib\site-packages%路径(安装的python的路径)下删除cv2的文件夹
再用pip重新安装opencv,pip install opencv****

 

========================

解决方法(可依次尝试):

1.重装cv2。(这是回答比较多的,多数情况有用)

先pip unintall opencv-python ,再pip intall opencv-python

2.设置环境变量。

参考:

Python-Opencv [ERROR: recursion is detected during loading of "cv2"_凌空的桨-CSDN博客

3.降低cv2版本。

pyinstaller和cv2的版本存在兼容问题。本人用的pyinstaller是4.7(python3.8.0),cv2是4.5.4.58,结果出错,把cv2版本降低到4.5.1.48再打包,就不报错了。

========================

https://wiki.archlinux.org/index.PHP/Arch_Linux_Archive#How_to_downgrade_one_package

 

========================

open cmd and use pip to install a different version:

 pip install opencv-python==4.5.3.56

========================

opencv-python      4.5.5.62

========================

 

REF

https://blog.csdn.net/weixin_44205803/article/details/100975762

https://blog.csdn.net/qq_44796370/article/details/121458744

 

iOS NSString,NSArray,NSDictionary属性关键字copy

iOS NSString,NSArray,NSDictionary属性关键字copy

创建了Person类,里面声明个name属性,关键字用copy

@property (nonatomic, copy)NSString *name;

在ViewController里给name赋值

NSMutableString *str = [[NSMutableString alloc] initWithString:@"iPhone"];

Person *person = [[Person alloc]init];

person.name = str;

[str appendString:@"6"];

NSLog(@"\n%@\n%@", str, person.name);

NSLog(@"\n%p\n%p", str, person.name);

打印结果

//iphone6   iphone   copy
//iphone6   iphone6  strong
//0x600001f8c030  //0xd5a5b31a6478d3f4  copy
//0x60000310ea90  //0x60000310ea90      strong
如果strong来修饰,如果NSMutableString的值赋值给NSString,那么只是将NSString指向了NSMutableString的内存地址,并对NSMUtbaleString计数器加一,此时,如果对NSMutableString进行修改,也会导致NSString的值修改,person的name属性会随着str的改变而改变,(试想一下一个人的名字怎么能在不知情的情况下被被改变那),破坏了封装性,原则上这是不允许的.如果是copy修饰的NSString对象,在用NSMutableString给他赋值时,会进行深拷贝,及把内容也给拷贝了一份,两者指向不同的位置,即使改变了NSMutableString的值,NSString的值也不会改变.所以用copy是为了安全,防止NSMutableString赋值给NSString时,前者修改引起后者值变化而用的.
以上规则不止适用于NSString,NSArray,NSDictionary等同理
//NSArray
NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:@[@"iphone6",@"iphone8"]];
    Person *p = [[Person alloc] init];
    p.arr = arr;
    [arr addObject:@"iphone x"];
    DLog(@"\n%@\n%@",arr,p.arr);
    /*
     (
     iphone6,
     iphone8,                    copy (arr)
     "iphone x"
     )
     (
     iphone6,                     copy(p.arr)
     iphone8
     )
     
     strong
     (
     iphone6,
     iphone8,
     "iphone x"
     )
     (
     iphone6,
     iphone8,
     "iphone x"
     )
     
     */
    DLog(@"\n%p\n%p",arr,p.arr);
    //copy   0x60000085d1d0       0x60000066d500
    //strong    0x600001ba84b0     0x600001ba84b0
 
NSDictionary
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"iphone6",@"key1",@"iphone7",@"key2", nil];
    Person *p = [[Person alloc] init];
    p.dict = dict;
    [dict addEntriesFromDictionary:@{@"key3":@"iphone8"}];
    DLog(@"\n%@\n%@",dict,p.dict);
    DLog(@"\n%p\n%p",dict,p.dict);
    /*
     copy
     {
     key1 = iphone6;
     key2 = iphone7;
     key3 = iphone8;
     }
     {
     key1 = iphone6;
     key2 = iphone7;
     }
     
     0x600003436800
     0x600003437700
     */
    /*
     strong
     {
     key1 = iphone6;
     key2 = iphone7;
     key3 = iphone8;
     }
     {
     key1 = iphone6;
     key2 = iphone7;
     key3 = iphone8;
     }
     0x60000276ee60
     0x60000276ee60
     */
 

关于NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换nsnumber转nsstring的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于20120810-课堂笔记--FoundationKit,NSRange,NSPoint,NSSize,NSRect,可变对象、cocoa-touch – NSDate作为NSDictionary的键、ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.、iOS NSString,NSArray,NSDictionary属性关键字copy的相关知识,请在本站寻找。

本文标签: