GVKun编程网logo

Objective-C 征途:String Party(征途java)

14

最近很多小伙伴都在问Objective-C征途:StringParty和征途java这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展ArchivingObjective-CObje

最近很多小伙伴都在问Objective-C 征途:String Party征途java这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Archiving Objective-C Objects with NSCoding、ie浏览器activexobject_ie8 object.defineproperty、ios – 如何将Swift String连接到Objective C NSString?、ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript等相关知识,下面开始了哦!

本文目录一览:

Objective-C 征途:String Party(征途java)

Objective-C 征途:String Party(征途java)

        这是Objective-C Party系列的第二篇,在这里,我会介绍数据类型的一个元素------字符串。字符串在开发编程的过程中,几乎是不可或缺的,其实Objective-C上的字符串与其他开发语言的字符串没有太大的分别,都是用来显现数据的!Cocoa中用来处理字符串的类就是Nsstring,我会在这里介绍Nsstring的创建、获取大小、比较、搜索字符、可变性,通过这一部分的学习,我们就可以大概掌握Nsstring日常出现得比较频繁的用法!

        1.创建

         用得比较多的一个类方法就stringWithFormat:,stringWithFormat:就是一个工程方法,它会根据你提供的参数创建新的Nsstring对象。当然还有其他很多方法来创建Nsstring,但就不再这里详述了,大家有兴趣可以查看IOS SDK帮助文档。

Nsstring *message = [[Nsstring alloc]initWithFormat:@"I am a Objective-C developer!"]; +(id)stringWithFormat:(Nsstring *)format,...;

         再看stringWithFormat:的原型,首先要注意的地方时,stringWithFormat:函数是用前导加号(+)来开始声明,就是说stringWithFormat:是一个类方法;第二个就原型末尾的参数表示省略号(...),表示该函数接受多个参数来格式化、生成新的Nsstring对象。

        

         2.获取大小

         这里获取大小所要说的内容比较少,就是获取字符串的字符长度,方法返回的是字符串中字符的个数(无符号整形),我直接上一段代码。

unsigned int length = [message length];

         

         3.比较

          比较是字符串常见的操作。但在比较的过程中,有些程序员习惯性地使用‘==’比较符号来比较两个字符串,如果在C#或者java开发语言上,可能会得到你想要的结果,但是Objective-C是C语言的扩展集,对字符串的处理也沿用了C语言的风格。使用‘==’来比较两个字符串,只是比较他们的指针值,而不是他们的值。

 

Nsstring *string1 = @"hello string1"; Nsstring *string2 ; string2 = [[Nsstring alloc]initWithFormat:@"%@",string1]; if([string1 isEqualToString:string2]) { NSLog(@"They are same!"); } else { NSLog(@"They are not the same!"); }

         

          如果按照这样的方法来输出结果:They are same!

          把isEqualToString:改成==,得到的结果却是:They are not the same!

          个中的原理,我想大家也很了解了!用于比较的方法还有一个,是很有用的:

 

- (NSComparisonResult) compare:(Nsstring *)string;

         

          compare:将接收对象和传递来的字符串逐个字符地进行比较,返回一个NSComparisonResult(就是一个enum型数据)来显示比较结果:

 

typedef enum _NSComparisonResult { NSOrderedAscending = -1,NSOrderedSame,NSOrderedDesending }NSComparisonResult;

        

         compare:进行的是区分大小写的比较。Objective-C还有一个方法compare:options:,它能给我们更多的控制权。options参数是一个位掩码。你可以使用或运算符(|)来添加选项标记。常用的选项:

          *  NSCaseInsensitiveSearch: 不区分大小写字符。

          *  NSLiteralSearch:  进行完全比较,区分大小写。

          *  NSNumericSearch:  比较字符串的字符个数,而不是字符值。

          如果你想忽略大小写和字符个数,那么就可以这样编写代码:

 

if([string1 compare:string2 options:NSCaseInsensitiveSearch|NSNumericSearch]==NSOrderedSame) { NSLog(@"They are match!"); }

        
        4.可变性

      Nsstring是不可变的,但是你可以用它来生成新的对象,查找字符等。但是你不能以删除或者增加字符的形式来改变它。所以Objective-C考虑到这一点,添加了Nsstring的一个子类:NSMutableString。使用NSMutableString可以预分配一块内存去存储它,这样后续的操作就会快很多。

 

NSMutableString *string3 = [NSMutableString stringWithCapacity:50]; [string3 appendString:@"I like "]; [string3 appendFormat:@"%@",@"IOS"];

        先看一下上面的例子。第一行,首先创建了NSMutableString对象,要注意的是50这个参数是要来有何用的呢?50是NSMutableString对象初始化的时候预分配的一个字符长度,也就是说,可以在NSMutableString对象中添加50个字符。这里并没有把对象的长度给定死,而是一个参考,如果对象的长度超过了50,便会自动增加自身的容量。

再看看第二、第三行代码,这里的原型是:

 

- (void) appendString: (Nsstring *) aString; - (void) appendFormat: (Nsstring *) format,...;

        都是用以给NSMutableString对象添加字符的方法。

Archiving Objective-C Objects with NSCoding

Archiving Objective-C Objects with NSCoding

For the seasoned Cocoa developer,this is a piece of cake. For newer developers,this can be a real pain,especially if you don't kNow what you're looking for. I get this question a decent amount,so I figured I'd put a quick guide together.

The Problem

You can't put just any object in a plist. This mainly gets people when they want to put something into NSUserDefaults and get an error (because NSUserDefaults archives to a plist under the hood).

Plists only support the core types: NsstringNSNumber,monospace">NSDate,monospace">NSData,monospace">NSArray,NSDictionary (and their CF buddies thanks to the toll-free bridge). The key here isNSDataYou can convert any object to NSData with the NSCoding protocol.

The Solution

There are two things you have to do: implement NSCoding and then use the archiver and unarchiver.

Implementing NSCoding

Say you have an object that looks like this:

@interface Note : NSObject {
    Nsstring *title;
    Nsstring *author;
    BOOL published;
}

@property (nonatomic, copy) Nsstring *title;
@property (nonatomic, copy) Nsstring *author;
@property (nonatomic) BOOL published;

@end
#import "Note.h"

@implementation Note

@synthesize title;
@synthesize author;
@synthesize published;

- (void)dealloc {
    [title release];
    [author release];
    [super dealloc];
}

@end

Pretty simple,right?

Now,all you have to do to implement NSCoding is the following:

NSObject <NSCoding> {
    Nsstring *title;
    Nsstring *author;
    dealloc {
    [title release];
    [author release];
    [super dealloc];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.title = [decoder decodeObjectForKey:@"title"];
        self.author = [decoder decodeObjectForKey:@"author"];
        self.published = [decoder decodeBoolForKey:@"published"];
    }
    return self;
}

- (encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:title forKey:@"time"];
    [encoder encodeObject:author forKey:@"author"];
    [encoder encodeBool:published forKey:@"published"];
}

@end

Pretty simple. All I did was add the <NSCoding> protocol delectation in the header file and initWithCoder: and encodeWithCoder: in the implementation. You use these methods to tell NSCoder how to encode your object into data. Notice how two of the variables are objects and one was a BOOL. You have to use different methods for non-objects. The NSCoder documentation has the full list.

Remember,that you can use NSCoder to archive your object however whatever you want. It doesn't have to just be all of the instance variables in your object,although that's what you'll do 90% of the time.

Using the Archiver and Unarchiver

This part is also really easy. Let's say you have an array of notes that you want to put into NSUserDefaults,here's the code:

// Given `notes` contains an array of Note objects
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notes];
[[NSUserDefaults standardUserDefaults] setobject:data forKey:@"notes"];

Unarchiving is just as easy:

NSData *notesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"notes"];
NSArray *notes = [NSKeyedUnarchiver unarchiveObjectWithData:notesData];

ie浏览器activexobject_ie8 object.defineproperty

ie浏览器activexobject_ie8 object.defineproperty

切记:ActiveX是微软的东西,故而这玩意儿只有IE才支持!

JavaScript中ActiveXObject对象是启用并返回 Automation 对象的引用,javaScript中利用ActiveXObject来创建FileSystemObject操作文件。 一、功能实现核心:FileSystemObject 对象 要在javascript中实现文件操作功能,主要就是依靠FileSystemobject对象。 二、FileSystemObject编程 使用FileSystemObject 对象进行编程很简单,一般要经过如下的步骤: 创建FileSystemObject对象、应用相关方法、访问对象相关属性 。 (一)创建FileSystemObject对象 创建FileSystemObject对象的代码只要1行: var fso = new ActiveXObject(“Scripting.FileSystemObject”); 上述代码执行后,fso就成为一个FileSystemObject对象实例。 (二)应用相关方法 创建对象实例后,就可以使用对象的相关方法了。比如,使用CreateTextFile方法创建一个文本文件: var fso = new ActiveXObject(“Scripting.FileSystemObject”); var f1 = fso.createtextfile(“c:\\myjstest.txt”,true); (三)访问对象相关属性 要访问对象的相关属性,首先要建立指向对象的句柄,这就要通过get系列方法实现:GetDrive负责获取驱动器信息,GetFolder负责获取文件夹信息,GetFile负责获取文件信息。比如,指向下面的代码后,f1就成为指向文件c:\test.txt的句柄: var fso = new ActiveXObject(“Scripting.FileSystemObject”); var f1 = fso.GetFile(“c:\\myjstest.txt”); 然后,使用f1访问对象的相关属性。比如: alert(“File last modified: ” + f1.DateLastModified); 执行上面最后一句后,将显示c:\myjstest.txt的最后修改日期属性值。 但有一点请注意:对于使用create方法建立的对象,就不必再使用get方法获取对象句柄了,这时直接使用create方法建立的句柄名称就可以: var fso = new ActiveXObject(“Scripting.FileSystemObject”); var f1 = fso.createtextfile(“c:\\myjstest.txt”,true); alert(“File last modified: ” + f1.DateLastModified); 三、操作驱动器(Drives) 使用FileSystemObject对象来编程操作驱动器(Drives)和文件夹(Folders)很容易,这就象在Windows文件浏览器中对文件进行交互操作一样,比如:拷贝、移动文件夹,获取文件夹的属性。 (一)Drives对象属性 Drive对象负责收集系统中的物理或逻辑驱动器资源内容,它具有如下属性: TotalSize:以字节(byte)为单位计算的驱动器大小。 AvailableSpace或FreeSpace:以字节(byte)为单位计算的驱动器可用空间。 DriveLetter:驱动器字母。 DriveType:驱动器类型,取值为:removable(移动介质)、fixed(固定介质)、network(网络资源)、CD-ROM或者RAM盘。 SerialNumber:驱动器的系列码。 FileSystem:所在驱动器的文件系统类型,取值为FAT、fat32和NTFS。 IsReady:驱动器是否可用。 ShareName:共享名称。 VolumeName:卷标名称。 Path和RootFolder:驱动器的路径或者根目录名称。 (二)Drive对象操作例程 下面的例程显示驱动器C的卷标、总容量和可用空间等信息: var fso, drv, s =””; fso = new ActiveXObject(“Scripting.FileSystemObject”); drv = fso.GetDrive(fso.GetDriveName(“c:\\”)); s += “Drive C:” + ” – “; s += drv.VolumeName + “\n”; s += “Total Space: ” + drv.TotalSize / 1024; s += ” Kb” + “\n”; s += “Free Space: ” + drv.FreeSpace / 1024; s += ” Kb” + “\n”; alert(s); 四、操作文件夹(Folders) 涉及到文件夹的操作包括创建、移动、删除以及获取相关属性。 Folder对象操作例程 : 下面的例程将练习获取父文件夹名称、创建文件夹、删除文件夹、判断是否为根目录等操作: var fso, fldr, s = “”; // 创建FileSystemObject对象实例 fso = new ActiveXObject(“Scripting.FileSystemObject”); // 获取Drive 对象 fldr = fso.GetFolder(“c:\\”); // 显示父目录名称 alert(“Parent folder name is: ” + fldr + “\n”); // 显示所在drive名称 alert(“Contained on drive ” + fldr.Drive + “\n”); // 判断是否为根目录 if (fldr.IsRootFolder) alert(“This is the root folder.”); else alert(“This folder isn’t a root folder.”); alert(“\n\n”); // 创建新文件夹 fso.CreateFolder (“C:\\Bogus”); alert(“Created folder C:\\Bogus” + “\n”); // 显示文件夹基础名称,不包含路径名 alert(“Basename = ” + fso.GetBaseName(“c:\\bogus”) + “\n”); // 删除创建的文件夹 fso.DeleteFolder (“C:\\Bogus”); alert(“Deleted folder C:\\Bogus” + “\n”); 五、操作文件(Files) 对文件进行的操作要比以上介绍的驱动器(Drive)和文件夹(Folder)操作复杂些,基本上分为以下两个类别:对文件的创建、拷贝、移动、删除操作和对文件内容的创建、添加、删除和读取操作。下面分别详细介绍。 (一)创建文件 一共有3种方法可用于创建一个空文本文件,这种文件有时候也叫做文本流(text stream)。 第一种是使用CreateTextFile方法。代码如下: var fso, f1; fso = new ActiveXObject(“Scripting.FileSystemObject”); f1 = fso.CreateTextFile(“c:\\testfile.txt”, true); 第二种是使用OpenTextFile方法,并添加上ForWriting属性,ForWriting的值为2。代码如下: var fso, ts; var ForWriting= 2; fso = new ActiveXObject(“Scripting.FileSystemObject”); ts = fso.OpenTextFile(“c:\\test.txt”, ForWriting, true); 第三种是使用OpenAsTextStream方法,同样要设置好ForWriting属性。代码如下: var fso, f1, ts; var ForWriting = 2; fso = new ActiveXObject(“Scripting.FileSystemObject”); fso.CreateTextFile (“c:\\test1.txt”); f1 = fso.GetFile(“c:\\test1.txt”); ts = f1.OpenAsTextStream(ForWriting, true); (二)添加数据到文件 当文件被创建后,一般要按照”打开文件->填写数据->关闭文件”的步骤实现添加数据到文件的目的。 打开文件可使用FileSystemObject对象的OpenTextFile方法,或者使用File对象的OpenAsTextStream方法。 填写数据要使用到TextStream对象的Write、WriteLine或者WriteBlankLines方法。在同是实现写入数据的功能下,这3者的区别在于:Write方法不在写入数据末尾添加新换行符,WriteLine方法要在最后添加一个新换行符,而WriteBlankLines则增加一个或者多个空行。 关闭文件可使用TextStream对象的Close方法。 (三)创建文件及添加数据例程 下面的代码将创建文件、添加数据、关闭文件几个步骤结合起来进行应用: var fso, tf; fso = new ActiveXObject(“Scripting.FileSystemObject”); // 创建新文件 tf = fso.CreateTextFile(“c:\\testfile.txt”, true); // 填写数据,并增加换行符 tf.WriteLine(“Testing 1, 2, 3.”) ; // 增加3个空行 tf.WriteBlankLines(3) ; // 填写一行,不带换行符 tf.Write (“This is a test.”); // 关闭文件 tf.Close(); (四)读取文件内容 从文本文件中读取数据要使用TextStream对象的Read、ReadLine或ReadAll 方法。Read方法用于读取文件中指定数量的字符;ReadLine方法读取一整行,但不包括换行符;ReadAll方法则读取文本文件的整个内容。读取的内容存放于字符串变量中,用于显示、分析。在使用Read或ReadLine方法读取文件内容时,如果要跳过一些部分,就要用到Skip或SkipLine方法。 下面的代码演示打开文件、填写数据,然后读取数据: var fso, f1, ts, s; var ForReading = 1; fso = new ActiveXObject(“Scripting.FileSystemObject”); // 创建文件 f1 = fso.CreateTextFile(“c:\\testfile.txt”, true); // 填写一行数据 f1.WriteLine(“Hello World”); f1.WriteBlankLines(1); // 关闭文件 f1.Close(); // 打开文件 ts = fso.OpenTextFile(“c:\\testfile.txt”, ForReading); // 读取文件一行内容到字符串 s = ts.ReadLine(); // 显示字符串信息 alert(“File contents = ” + s + “”); // 关闭文件 ts.Close(); (五)移动、拷贝和删除文件 对于以上三种文件操作,javascript各有两种对应的方法:File.Move 或 FileSystemObject.MoveFile用于移动文件;File.copy 或 FileSystemObject.copyFile用于拷贝文件;File.Delete 或 FileSystemObject.DeleteFile用于删除文件。 下面的代码演示在驱动器C的根目录下创建一个文本文件,填写一些内容,然后将文件移动到\tmp目录下,再在目录\temp下面建立一个文件拷贝,最后删除这两个目录的文件: var fso, f1, f2, s; fso = new ActiveXObject(“Scripting.FileSystemObject”); f1 = fso.CreateTextFile(“c:\\testfile.txt”, true); // 写一行 f1.Write(“This is a test.”); // 关闭文件 f1.Close(); // 获取C:\根目录下的文件句柄 f2 = fso.GetFile(“c:\\testfile.txt”); // 移动文件到\tmp目录下 f2.Move (“c:\\tmp\\testfile.txt”); // 拷贝文件到\temp目录下 f2.copy (“c:\\temp\\testfile.txt”); // 获取文件句柄 f2 = fso.GetFile(“c:\\tmp\\testfile.txt”); f3 = fso.GetFile(“c:\\temp\\testfile.txt”); // 删除文件 f2.Delete(); f3.Delete(); (六)总结 在用户不知情的情况下,ActiveXObject可以完成对文本的一系列操作;由于是IE浏览器对当前操作电脑的磁盘文件进行操作,所以与项目部署环境无关(无论项目服务器在windows还是linux等);而且还有一点提醒大家,由于涉及到在浏览器中进行文件读写这样的高级操作,对于默认的浏览器安全级别而言,在代码运行前都会有一个信息提示,这点请在实际环境中提示访问者注意。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183275.html原文链接:https://javaforall.cn

ios – 如何将Swift String连接到Objective C NSString?

ios – 如何将Swift String连接到Objective C NSString?

我吃疯狂药吗直接从文档中删除:

“Swift automatically bridges between the String type and the Nsstring class. This means that anywhere you use an Nsstring object,you can use a Swift String type instead and gain the benefits of both types—the String type’s interpolation and Swift-designed APIs and the Nsstring class’s broad functionality. For this reason,you should almost never need to use the Nsstring class directly in your own code. In fact,when Swift imports Objective-C APIs,it replaces all of the Nsstring types with String types. When your Objective-C code uses a Swift class,the importer replaces all of the String types with Nsstring in imported API.

要启用字符串桥接,只需导入基础“.

我做了这个…考虑:

import Foundation

var str = "Hello World"

var range = str.rangeOfString("e")

// returns error: String does not contain member named: rangeOfString()

然而:

var str = "Hello World" as Nsstring

var range = str.rangeOfString("e")

// returns correct (2,1)

我错过了什么吗?

解决方法

你已经有你的问题的答案.你错过了演员.写Swift代码时,这样一个语句
var str = "Hello World"

创建一个Swift字符串,而不是Nsstring.要使其作为Nsstring工作,您应该在使用之前使用as操作符将其转换为Nsstring.

这不同于调用在Objective-C中编写的方法,并提供一个String而不是一个Nsstring作为参数.

ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript

ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript

我最近发现了 eero programming lanugage
我发现它的概念非常有趣.它似乎主要是语法糖,看起来很可读.

虽然它不直接编译到Objective-C,但它声称生成与Objective-C相同的二进制代码

Eero compiles down to the same binary code as Objective-C

Eero offers excellent,nearly seamless interoperability with
Objective-C,C,and C++.

我发现这种方法非常有趣,我想知道是否有类似的编程语言和项目提供与Objective-C和iOS的大腿集成.
我正在为Objective-C寻找类似Coffeescript的东西.

解决方法

虽然使用eero的主要方法是将其编译为本机代码,但它也支持源到源的转换(从eero到标准的Objective-C/C++).有关详细信息,请参阅 https://github.com/eerolanguage/eero/wiki/Translator.

它确实需要记录在一个更明显的地方……

我们今天的关于Objective-C 征途:String Party征途java的分享已经告一段落,感谢您的关注,如果您想了解更多关于Archiving Objective-C Objects with NSCoding、ie浏览器activexobject_ie8 object.defineproperty、ios – 如何将Swift String连接到Objective C NSString?、ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript的相关信息,请在本站查询。

本文标签: