Copy a portion of an NSMutableData to a new NSMutableData object
I am reading in a large file into an NSMutableData object.
var lBuffer : NSMutableData = try NSMutableData(contentsOfFile: pFilePath)
I would like to get a portion of the NSMutableData object to work with at a time. So I am trying to use .subdata(with: NSRange), but am getting an error.
var lBufferChunk : NSMutableData = lBuffer.subdata(with: NSRange(location: 0, length: lSizeOfChunk)) as! NSMutableData
The error I get is:
Could not cast value of type 'NSSubrangeData' (0x7fffa6ea3ef8) to 'NSMutableData' (0x7fffa6a403f8)
According to the documentation, .subdata(with: NSRange) should return a Data object.
How can I get it to a Data object to cast it back to NSMutableData? Or is there a better way of getting a portion/fragment out of a large NSMutableData?
swift macos nsdata
add a comment |
I am reading in a large file into an NSMutableData object.
var lBuffer : NSMutableData = try NSMutableData(contentsOfFile: pFilePath)
I would like to get a portion of the NSMutableData object to work with at a time. So I am trying to use .subdata(with: NSRange), but am getting an error.
var lBufferChunk : NSMutableData = lBuffer.subdata(with: NSRange(location: 0, length: lSizeOfChunk)) as! NSMutableData
The error I get is:
Could not cast value of type 'NSSubrangeData' (0x7fffa6ea3ef8) to 'NSMutableData' (0x7fffa6a403f8)
According to the documentation, .subdata(with: NSRange) should return a Data object.
How can I get it to a Data object to cast it back to NSMutableData? Or is there a better way of getting a portion/fragment out of a large NSMutableData?
swift macos nsdata
add a comment |
I am reading in a large file into an NSMutableData object.
var lBuffer : NSMutableData = try NSMutableData(contentsOfFile: pFilePath)
I would like to get a portion of the NSMutableData object to work with at a time. So I am trying to use .subdata(with: NSRange), but am getting an error.
var lBufferChunk : NSMutableData = lBuffer.subdata(with: NSRange(location: 0, length: lSizeOfChunk)) as! NSMutableData
The error I get is:
Could not cast value of type 'NSSubrangeData' (0x7fffa6ea3ef8) to 'NSMutableData' (0x7fffa6a403f8)
According to the documentation, .subdata(with: NSRange) should return a Data object.
How can I get it to a Data object to cast it back to NSMutableData? Or is there a better way of getting a portion/fragment out of a large NSMutableData?
swift macos nsdata
I am reading in a large file into an NSMutableData object.
var lBuffer : NSMutableData = try NSMutableData(contentsOfFile: pFilePath)
I would like to get a portion of the NSMutableData object to work with at a time. So I am trying to use .subdata(with: NSRange), but am getting an error.
var lBufferChunk : NSMutableData = lBuffer.subdata(with: NSRange(location: 0, length: lSizeOfChunk)) as! NSMutableData
The error I get is:
Could not cast value of type 'NSSubrangeData' (0x7fffa6ea3ef8) to 'NSMutableData' (0x7fffa6a403f8)
According to the documentation, .subdata(with: NSRange) should return a Data object.
How can I get it to a Data object to cast it back to NSMutableData? Or is there a better way of getting a portion/fragment out of a large NSMutableData?
swift macos nsdata
swift macos nsdata
edited Nov 21 at 3:29
rmaddy
238k27309375
238k27309375
asked Nov 21 at 2:12
Tagnal
375
375
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If you really wanted to, you could cast the result of calling lBuffer.subdata
to an NSData and then call mutableCopy()
and force-cast the result to NSMutableData. But don't! Don't even use NSData or NSMutableData in the first place. This is Swift. Use Data.
var lBuffer = Data(contentsOf: myFileURL)
Now just call subdata(in:)
var lBufferChunk = lBuffer.subdata(in: myRange)
Or subscript
var lBufferChunk = lBuffer[myRange]
The var
makes it mutable.
or simplylBuffer[myRange]
– Leo Dabus
Nov 21 at 3:29
The reason why I'm trying to use NSMutableData is because I need to pass it to an objective-c function that takes in a NSMutableData object.
– Tagnal
Nov 21 at 20:23
What a strange function that would be! But if that's really the case, you can pass a Data to that function as I already explained. For example to passlBufferChunk
where an NSMutableData is expected you would do what I already said: cast it to NSData and then callmutableCopy()
and force-cast to NSMutableData.
– matt
Nov 21 at 21:00
add a comment |
So, I went a different route to solve my issue. As you can see, I wanted to read in a file to a NSMutableData object, but wanted to copy smaller portions of it at a time (in the case of large files). Instead of reading a range of one Data object and passing it to the other, I decided to use an input stream instead.
var lInputStream : InputStream = InputStream(fileAtPath: pFilePath)!
var lBuffer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: lBufferSize)
lInputStream.open()
while lInputStream.hasBytesAvailable {
let read = lInputStream.read(lBuffer, maxLength: lBufferSize)
var dataChunk = NSMutableData(length: 0)
dataChunk?.append(lBuffer, length: read)
...
}
lInputStream.close()
Matt's answer is correct for the way the question was originally asked and I've marked it as so. Just wanted to share what I ended up doing in the end.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404392%2fcopy-a-portion-of-an-nsmutabledata-to-a-new-nsmutabledata-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you really wanted to, you could cast the result of calling lBuffer.subdata
to an NSData and then call mutableCopy()
and force-cast the result to NSMutableData. But don't! Don't even use NSData or NSMutableData in the first place. This is Swift. Use Data.
var lBuffer = Data(contentsOf: myFileURL)
Now just call subdata(in:)
var lBufferChunk = lBuffer.subdata(in: myRange)
Or subscript
var lBufferChunk = lBuffer[myRange]
The var
makes it mutable.
or simplylBuffer[myRange]
– Leo Dabus
Nov 21 at 3:29
The reason why I'm trying to use NSMutableData is because I need to pass it to an objective-c function that takes in a NSMutableData object.
– Tagnal
Nov 21 at 20:23
What a strange function that would be! But if that's really the case, you can pass a Data to that function as I already explained. For example to passlBufferChunk
where an NSMutableData is expected you would do what I already said: cast it to NSData and then callmutableCopy()
and force-cast to NSMutableData.
– matt
Nov 21 at 21:00
add a comment |
If you really wanted to, you could cast the result of calling lBuffer.subdata
to an NSData and then call mutableCopy()
and force-cast the result to NSMutableData. But don't! Don't even use NSData or NSMutableData in the first place. This is Swift. Use Data.
var lBuffer = Data(contentsOf: myFileURL)
Now just call subdata(in:)
var lBufferChunk = lBuffer.subdata(in: myRange)
Or subscript
var lBufferChunk = lBuffer[myRange]
The var
makes it mutable.
or simplylBuffer[myRange]
– Leo Dabus
Nov 21 at 3:29
The reason why I'm trying to use NSMutableData is because I need to pass it to an objective-c function that takes in a NSMutableData object.
– Tagnal
Nov 21 at 20:23
What a strange function that would be! But if that's really the case, you can pass a Data to that function as I already explained. For example to passlBufferChunk
where an NSMutableData is expected you would do what I already said: cast it to NSData and then callmutableCopy()
and force-cast to NSMutableData.
– matt
Nov 21 at 21:00
add a comment |
If you really wanted to, you could cast the result of calling lBuffer.subdata
to an NSData and then call mutableCopy()
and force-cast the result to NSMutableData. But don't! Don't even use NSData or NSMutableData in the first place. This is Swift. Use Data.
var lBuffer = Data(contentsOf: myFileURL)
Now just call subdata(in:)
var lBufferChunk = lBuffer.subdata(in: myRange)
Or subscript
var lBufferChunk = lBuffer[myRange]
The var
makes it mutable.
If you really wanted to, you could cast the result of calling lBuffer.subdata
to an NSData and then call mutableCopy()
and force-cast the result to NSMutableData. But don't! Don't even use NSData or NSMutableData in the first place. This is Swift. Use Data.
var lBuffer = Data(contentsOf: myFileURL)
Now just call subdata(in:)
var lBufferChunk = lBuffer.subdata(in: myRange)
Or subscript
var lBufferChunk = lBuffer[myRange]
The var
makes it mutable.
edited Nov 21 at 5:03
answered Nov 21 at 2:32
matt
322k45519720
322k45519720
or simplylBuffer[myRange]
– Leo Dabus
Nov 21 at 3:29
The reason why I'm trying to use NSMutableData is because I need to pass it to an objective-c function that takes in a NSMutableData object.
– Tagnal
Nov 21 at 20:23
What a strange function that would be! But if that's really the case, you can pass a Data to that function as I already explained. For example to passlBufferChunk
where an NSMutableData is expected you would do what I already said: cast it to NSData and then callmutableCopy()
and force-cast to NSMutableData.
– matt
Nov 21 at 21:00
add a comment |
or simplylBuffer[myRange]
– Leo Dabus
Nov 21 at 3:29
The reason why I'm trying to use NSMutableData is because I need to pass it to an objective-c function that takes in a NSMutableData object.
– Tagnal
Nov 21 at 20:23
What a strange function that would be! But if that's really the case, you can pass a Data to that function as I already explained. For example to passlBufferChunk
where an NSMutableData is expected you would do what I already said: cast it to NSData and then callmutableCopy()
and force-cast to NSMutableData.
– matt
Nov 21 at 21:00
or simply
lBuffer[myRange]
– Leo Dabus
Nov 21 at 3:29
or simply
lBuffer[myRange]
– Leo Dabus
Nov 21 at 3:29
The reason why I'm trying to use NSMutableData is because I need to pass it to an objective-c function that takes in a NSMutableData object.
– Tagnal
Nov 21 at 20:23
The reason why I'm trying to use NSMutableData is because I need to pass it to an objective-c function that takes in a NSMutableData object.
– Tagnal
Nov 21 at 20:23
What a strange function that would be! But if that's really the case, you can pass a Data to that function as I already explained. For example to pass
lBufferChunk
where an NSMutableData is expected you would do what I already said: cast it to NSData and then call mutableCopy()
and force-cast to NSMutableData.– matt
Nov 21 at 21:00
What a strange function that would be! But if that's really the case, you can pass a Data to that function as I already explained. For example to pass
lBufferChunk
where an NSMutableData is expected you would do what I already said: cast it to NSData and then call mutableCopy()
and force-cast to NSMutableData.– matt
Nov 21 at 21:00
add a comment |
So, I went a different route to solve my issue. As you can see, I wanted to read in a file to a NSMutableData object, but wanted to copy smaller portions of it at a time (in the case of large files). Instead of reading a range of one Data object and passing it to the other, I decided to use an input stream instead.
var lInputStream : InputStream = InputStream(fileAtPath: pFilePath)!
var lBuffer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: lBufferSize)
lInputStream.open()
while lInputStream.hasBytesAvailable {
let read = lInputStream.read(lBuffer, maxLength: lBufferSize)
var dataChunk = NSMutableData(length: 0)
dataChunk?.append(lBuffer, length: read)
...
}
lInputStream.close()
Matt's answer is correct for the way the question was originally asked and I've marked it as so. Just wanted to share what I ended up doing in the end.
add a comment |
So, I went a different route to solve my issue. As you can see, I wanted to read in a file to a NSMutableData object, but wanted to copy smaller portions of it at a time (in the case of large files). Instead of reading a range of one Data object and passing it to the other, I decided to use an input stream instead.
var lInputStream : InputStream = InputStream(fileAtPath: pFilePath)!
var lBuffer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: lBufferSize)
lInputStream.open()
while lInputStream.hasBytesAvailable {
let read = lInputStream.read(lBuffer, maxLength: lBufferSize)
var dataChunk = NSMutableData(length: 0)
dataChunk?.append(lBuffer, length: read)
...
}
lInputStream.close()
Matt's answer is correct for the way the question was originally asked and I've marked it as so. Just wanted to share what I ended up doing in the end.
add a comment |
So, I went a different route to solve my issue. As you can see, I wanted to read in a file to a NSMutableData object, but wanted to copy smaller portions of it at a time (in the case of large files). Instead of reading a range of one Data object and passing it to the other, I decided to use an input stream instead.
var lInputStream : InputStream = InputStream(fileAtPath: pFilePath)!
var lBuffer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: lBufferSize)
lInputStream.open()
while lInputStream.hasBytesAvailable {
let read = lInputStream.read(lBuffer, maxLength: lBufferSize)
var dataChunk = NSMutableData(length: 0)
dataChunk?.append(lBuffer, length: read)
...
}
lInputStream.close()
Matt's answer is correct for the way the question was originally asked and I've marked it as so. Just wanted to share what I ended up doing in the end.
So, I went a different route to solve my issue. As you can see, I wanted to read in a file to a NSMutableData object, but wanted to copy smaller portions of it at a time (in the case of large files). Instead of reading a range of one Data object and passing it to the other, I decided to use an input stream instead.
var lInputStream : InputStream = InputStream(fileAtPath: pFilePath)!
var lBuffer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: lBufferSize)
lInputStream.open()
while lInputStream.hasBytesAvailable {
let read = lInputStream.read(lBuffer, maxLength: lBufferSize)
var dataChunk = NSMutableData(length: 0)
dataChunk?.append(lBuffer, length: read)
...
}
lInputStream.close()
Matt's answer is correct for the way the question was originally asked and I've marked it as so. Just wanted to share what I ended up doing in the end.
answered Nov 27 at 22:44
Tagnal
375
375
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404392%2fcopy-a-portion-of-an-nsmutabledata-to-a-new-nsmutabledata-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown