Objective-C equivalent of Java Vector/ ArrayList

本文探讨了 Objective-C 中等同于 Java Vector 的数据结构,重点介绍了 NSMutableArray 和 NSArray 的区别与使用场景。同时,文章还讨论了 Objective-C 中的 ArrayList 相当于 Java 的 ArrayList,并提供了 Cocoa 框架下集合类的概述。最后,文章展示了如何将 Java 中的 MessageDigest 类功能转换为 Objective-C 的实现,涉及使用 OpenSSL 库进行 SHA-256 消息摘要计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://stackoverflow.com/questions/2271492/objective-c-equivalent-of-java-vector

 

ask:

What is the equivalent for Vector's of Java in Objective-C?

 
answer:
Try using NSMutableArray.
 

The closest thing you will find is NSMutableArray, execpt that contrary to java Vector, it is not thread safe. If you do not need thread safety, NSMutableArray is nice. I suspect that if you use java vector instead of List, it is that you need thread safaty, then in objective-C, you should probably use NSArray. THe API is slightly different, since the add operation of an element to a NSArray returns a new array instance, but it is thread safe, seehttp://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

NSArray:http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/cl/NSArray NSMutableArray:http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableArray

 

 

In C++ an Array is basically just a pointer to a contiguous block of data---a series of elements. It offers no built-in methods, or higher functionality.

int intArr[]={0,1,2,3};

is the same as

int*intArr =(int*)malloc(4*sizeof(int));
for(int i =0; i <4; i++){ intArr[i]= i;}

A vector (std::vector), on the other hand, is a container for elements (basically like an array) which also offers additional built in methods (see: http://www.cplusplus.com/reference/stl/vector/) such as

vector<int> intArr;
for(int i =0; i <4; i++){ intArr.push_back(i);}
// this yields the same content; i.e. intArr = {0,1,2,3}

Both arrays and vectors can be used on any type of objects, int, double, 'MySpacePirateWizardClass' etc. The big bonus of vectors is the additional functionality from built-in functions like:

int arrSize = intArr.size();  // vector also includes useful information
int*firstElement = intArr.begin();    // methods for creating pointers to elements
intArr
.delete(0);             // modify elements
intArr
.insert(0,2);          // modify vector
// now: intArr = {2,1,2,3}

etc etc.

When I know I'm not going to be short on space (or looking at massive amounts of data), I always use vectors because they're so more convenient (even just the size() method alone is reason enough).

 

 

ask:

Objective C equivalent to Java's ArrayList

 

Just wondering if there is an equivalent to Java's ArrayList in Objective-C?

Or something that can be used to store objects/data that does not have a set size.

 

As others have pointed out, NSArray/NSMutableArray are what you're looking for.

Coming from the Java world, you may find that Cocoa's collection offerings feel quite paltry. In fact, the functionality is quite extensive. The NSArrayNSDictionary, and NSSet are, in fact, class clusters, meaning that the public API is an "abstract" class. When you initialize one of the collections, what you get back is, in fact, a concrete implementation tailored for the data you provide. These implementations can also change the concrete implementation at run time if the data changes (e.g. it grows in size). This is all possible due to Objective-C's much more dynamic run time than Java's static typing (and security) will allow. The class cluster strategy thus hides many of the implementations of, e.g. thejava.util.List interface, behind a single API.

The Cocoa frameworks are somewhat limited in compound data structures (i.e. those built on top of "primitive" arrays, sets, and dictionaries). You may find that the excellent, open sourceCHDataStructures fills in many of the gaps.

 

 

 

 

Objective C equivalent of MessageDigest in Java?

 

I'm taking a task converting Java code to Objective C.

This is the code in Java that I have to convert:

privateString getHash(String input) 
{
   
String ret =null;
   
try
   
{
       
MessageDigest md =MessageDigest.getInstance("SHA-256");

       
byte[] bs = md.digest(input.getBytes("US-ASCII"));


       
StringBuffer sb =newStringBuffer();
       
for(byte b : bs)
       
{
           
String bt =Integer.toHexString(b &0xff);
           
if(bt.length()==1)
           
{
                sb
.append("0");
           
}
            sb
.append(bt);
       
}
        ret
= sb.toString();
   
}
   
catch(Exception e)
   
{
   
}
   
return ret;
}

Specifically, what can I use in Objective C which has the same functionality as the MessageDigestclass?

You want the OpenSSL library. See the answers to generate sha256 with openssl and C++ for an example (the title says C++, but OpenSSL is just a basic C library).

 

I found a apple framework to support SHA-256 in stackoverflow.com. Thx stackoverflow :)

CommonCrypto/CommonDigest.h

and I realized that I can use this function:

CC_SHA256(const void*data, CC_LONG len,unsignedchar*md)

CC_SHA256_Final
(unsignedchar*md, CC_SHA256_CTX *c)

CC_SHA256_Init
(CC_SHA256_CTX *c)

CC_SHA256_Update
(CC_SHA256_CTX *c,constvoid*data, CC_LONG len)

So I can go on my task except this Java code.

byte[] bs = md.digest(input.getBytes("US-ASCII"));

and I want to know that any Objective C expression of Java circular code below?

for(byte b : bs)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值