http://stackoverflow.com/questions/2271492/objective-c-equivalent-of-java-vector
ask:
What is the equivalent for Vector's of Java in Objective-C?
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 NSArray
, NSDictionary
, 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 MessageDigest
class?
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)