If you don’t know Swift yet, checkout the Swift Tour by Apple. Then come back.
Welcome back, you now know basic Swift, congratulations.
Noze.io is pretty similar to Node.js. You can import fs
, http
, net
, etc
and most functions should be named the same like in Node.js. There is concat
,
nextTick
and console.log
.
It is, however, not exactly the same. A few key differences:
- Modules are named similar but work a little different
- they are neither objects nor functions
- you don’t
require
them, butimport
them - you can’t rename them (ala
let blub = require('blob')
) - they do not carry resources
- they have no
__dirname
- you cannot
require
arbitrary JSON files (read them viajsonfile.readFile
etc.)
- Get used to trailing closures, just attach a block to an event
(
stream.onReadable { ... }
, notstream.on('readable', function() { })
) - Noze.io only implements Node v3 streams, that is: the
readable
event and the matchingread()
method. The old-styledata
events are not available. - Swift uses ARC while Node.js has a GC. You can mostly ignore that, but once your code gets more complex you need to consider retain cycles and such.
- Noze.io streams are typed and always work in batches. In Node.js only byte streams process batches, the Node.js “object-mode” is one-object-at-a-time.
- Being typed, a ‘byte’ (UInt8) stream and a
Character
orString
stream are different things. Hence noencoding
parameters and such. If you have a byte stream but you want to work with chars, pipe it throughutf8
orreadlines
(e.g.stdin | readlines
). - Noze.io overloads the pipe
|
operator (this is the only one), it is the same like the.pipe()
method (which is also available). - In Noze.io you can directly pipe from sequences into streams, like:
allFetchedRecords | record2html | response
Examples
Node original:
fs.readFile('example_log.txt', function (err, logData) {
if (err) { console.error("failed:", err); return; }
console.log("got data:", logData);
});
The same in Noze.io:
fs.readFile("example_log.txt") { err, logData in
if err { console.error("failed:", err); return }
console.log("got data:", logData)
}