Start NSInputStream at certain location [duplicate] - cocoa-touch

This question already has an answer here:
Do the stream classes in Cocoa support seeking?
(1 answer)
Closed 8 years ago.
I have an input stream and I want to start reading bytes at a certain location. Is there any way I can do that? Say I want to read 1024 bytes at location n, n being the nth byte in the file. How do I achieve this? I've looked at the documentation and there doesn't appear to be way to do this.

You need to use the below api for reading :-
+ (id)inputStreamWithFileAtPath:(NSString *)path
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len;

Related

getBytes exceeds data length, but the range is obviously less than the length [duplicate]

This question already has an answer here:
Out of bounds Problem with attributedSubstringFromRange
(1 answer)
Closed 6 years ago.
I am getting this output:
[NSConcreteData getBytes:range:]: range {2605022, 2605022} exceeds data length 3907534'
From this statement:
for(uint i = data.length*2/3; i<data.length; i++){
// NSLog(#"i: %u",i);
[data getBytes:buffer range:NSMakeRange(i,i)];
}
What I am doing wrong? I have never used Objective-C before, so I really don't know what I'm doing.
For those in my shoes, NSMakeRange is not start and end. It is start and length. A similar question was posed here: http://idevapps.com/forum/showthread.php?tid=181

vhdl code (while loop) [duplicate]

This question already has answers here:
vhdl code (for loop)
(2 answers)
Closed 8 years ago.
description:
I would like to write a vhdl while loop that will find the largest integer in an array [A] of 20 integers.
Question:
what should my algorithm look like, to input where the sequential statements are?
My vhdl code:
highnum: WHILE i LOOP
if
(arr[i]>arr[HighestSoFar]){HighestSoFar=i;}20
i<= i + 1;
end if;
exit;
END LOOP highnum;
This does not need to be synthesizable but I dont know how to form this for loop, in fact im not sure what the syntax would look like. a detailed example explaining how to would be appreciated.
TLDR: Don't.
While loops have no fixed bound on the number of iterations of the loop. Any attempt to synthesise them would have to generate a runtime-variable amount of hardware : hopefully you can see this as absurd until we learn to make dynamically growing FPGAs.
As it doesn't have to be synthesisable you CAN use a While loop and it will work pretty much as you would expect if you have used other programming languages, but it offers no advantages for this task over the For loop.
So use a For loop instead.
You are requesting examples. Wikibooks has plenty of examples:
The for loop structure:
[label:] [WHILE <condition> | FOR <condition>] LOOP
[statements]
[exit;]
[next;]
END LOOP [label];

What is the crittercism stacktrace meaning? [duplicate]

This question already has answers here:
How to read objective-c stack traces
(3 answers)
Closed 9 years ago.
It's the first time I use crittercism and when I take a look to stacktrace there is something like :
0x000b24d9 __32-[MenuViewController loadOrders]_block_invoke + 189
What does mean the + 189 ?
I checked, this is not the line number.
Thanks !
In other debuggers (i.e. WinDbg) it's the byte offset within that method.
The number is the bytes offset from the entry point of the method to the instruction which caused the app to crash. Read this for more info on how to understand crash reports.

What does a square-bracketed index after an NSArray mean? [duplicate]

This question already has answers here:
What are the details of "Objective-C Literals" mentioned in the Xcode 4.4 release notes?
(4 answers)
Closed 8 years ago.
Going through iTunes U Developing iOS 7 Apps for iPhone and iPad and in Lecture 3 slides, on page 120, there's a Quiz question that asks what the following line of code does. Frankly, I'm a bit stumped, and hoped someone could break it down.
cardA.contents = #[cardB.contents,cardC.contents][[cardB match:#[cardC]] ? 1 : 0];
So, I get the first part, cardA.contents = a new array with cardB.contents and cardC.contents in the array. But, next comes (I guess??) an index that returns either 1 or 0 depending if cardB matches an array that includes cardC.
Here's what I don't "get", and maybe it's just a syntax issue.... is what this does?
How is
cardA.contents = #[cardB.contents,cardC.contents][0];
or
cardA.contents = #[cardB.contents,cardC.contents][1];
Valid? Or, did I miss something?
Your understanding is completely correct; you're just missing one bit of syntax. Subscripted access of NSArrays with the array[index] form is part of the "collection literals" feature introduced by Clang a little while back.
It's transformed by the compiler into a call to [array objectAtIndexedSubscript:index].
As usual, in writing this out, it makes sense. It's literally saying, that if cardB matched cardC, use an index of [1] (cardC), if not, use an index of [0] (cardB)
So,
cardA.contents = cardB.contents // if does NOT match
cardA.contents = cardC.contents // if matches
(based on the index).
Duh.. Sorry for a silly question...

Reading SWF Header with Objective-C

I am trying to read the header of an SWF file using NSData.
According to SWF format specification I need to access movie's width and height reading bits, not bytes, and I couldn't find a way to do it in Obj-C
Bytes 9 thru ?: Here is stored a RECT (bounds of movie). It must be read in binary form. First of all, we will transform the first byte to binary: "01100000"
The first 5 bits will tell us the size in bits of each stored value: "01100" = 12
So, we have 4 fields of 12 bits = 48 bits
48 bits + 5 bits (header of RECT) = 53 bits
Fill to complete bytes with zeroes, till we reach a multiple of 8. 53 bits + 3 alignment bits = 56 bits (this RECT is 7 bytes length, 7 * 8 = 56)
I use this formula to determine all this stuff:
Where do I start?
ObjC is a superset of C: You can run C code alongside ObjC with no issues.
Thus, you could use a C-based library like libming to read bytes from your SWF file.
If you need to shuffle bytes into an NSData object, look into the -dataWithBytes:length: method.
Start by looking for code with a compatible license that already does what you want. C libraries can be used from Obj-C code simply by linking them in (or arranging for them to be dynamically linked in) and then calling their functions.
Failing that, start by looking at the Binary Data Programming Guide for Cocoa and NSData Class Reference. You'd want to pull out the bytes that contain the bits you're interested in, then use bit masking techniques to extract the bits you care about. You might find the BitTst(), BitSet(), and BitClr() functions and their friends useful, if they're still there in Snow Leopard; I'm not sure whether they ended up in the démodé parts of Carbon or not. There are also the Posix setbit(), clrbit(), isset(), and isclr() macros defined in . Then, finally, there are the C bitwise operators: ^, |, &, ~, <<, and >>.