Vyacheslav Vyacheslav - 1 year ago 62
Swift Question

While with assignment in swift

what is the proper way to convert this ObjC code into Swift?

while ((size = [inputdata readWithByteArray:buf]) != -1) {

I need similar to this (from here):

while let super = view?.superview {
view = super

but in my case i have to compare the value with
instead of

Answer Source

You can use while case with a binding pattern plus a boolean condition:

while case let size = inputdata.readWithByteArray(&buf), size != -1 {
    // process data


(Previous answers:) A straight-forward implementation would look like this:

var size = inputdata.readWithByteArray(&buf)
while size != -1 {
    // process data ...

    size = inputdata.readWithByteArray(&buf)

If the code-duplication bothers you then you can write it also as

var size: Int
while (size = inputdata.readWithByteArray(&buf), size).1 != -1 {
    // process data


Here the (...).1 expression evaluates to the second tuple element, i.e. to size, and the first tuple element is only evaluated for its side-effect. (This is possible in Swift because expressions are evaluated from left to right, as stated by Chris Lattner in https://forums.developer.apple.com/thread/20001#63783.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download