MOCKSTACKS
EN
Questions And Answers

More Tutorials









Swift Function reference


This section details functions that are available for use in the Swift language.

arg


Takes a command line parameter name as a string parameter and an optional default value and returns the value of that string parameter from the command line. If no default value is specified and the command line parameter is missing, an error is generated. If a default value is specified and the command line parameter is missing, @arg will return the default value.

Command line parameters recognized by @arg begin with exactly one hyphen and need to be positioned after the script name.

For example:


trace(arg("myparam"));
trace(arg("optionalparam", "defaultvalue"));
$ swift arg.swift -myparam=hello
Swift v0.3-dev r1674 (modified locally)
RunID: 20080220-1548-ylc4pmda
Swift trace: defaultvalue
Swift trace: hello

extractInt


extractInt(file) will read the specified file, parse an integer from the file contents and return that integer.

extractFloat


Similar to extractInt, extractFloat(file) will read the specified file, parse a float from the file contents and return that float.

filename


filename(v) will return a string containing the filename(s) for the file(s) mapped to the variable v. When more than one filename is returned, the filenames will be space separated inside a single string return value.

filenames


filenames(v) will return multiple values containing the filename(s) for the file(s) mapped to the variable v.

length


length(array) will return the length of an array in Swift. This function will wait for all elements in the array to be written before returning the length.

readData


readData will read data from a specified file and assign it to Swift variable. The format of the input file is controlled by the type of the return value. For scalar return types, such as int, the specified file should contain a single value of that type. For arrays of scalars, the specified file should contain one value per line. For complex types of scalars, the file should contain two rows. The first row should be structure member names separated by whitespace. The second row should be the corresponding values for each structure member, separated by whitespace, in the same order as the header row. For arrays of structs, the file should contain a heading row listing structure member names separated by whitespace. There should be one row for each element of the array, with structure member elements listed in the same order as the header row and separated by whitespace. The following example shows how readData() can be used to populate an array of Swift struct-like complex type:

type Employee{
string name;
int id;
string loc;
}
Employee emps[] = readData("emps.txt");

Where the contents of the "emps.txt" file are:

name id loc
Thomas 2222 Chicago
Gina 3333 Boston
Anne 4444 Houston

This will result in the array "emps" with 3 members. This can be processed within a Swift script using the foreach construct as follows:

foreach emp in emps{
tracef("Employee %s lives in %s and has id %d", emp.name, emp.loc, emp.id);
}



Conclusion

In this page (written and validated by ) you learned about Swift Function reference . What's Next? If you are interested in completing Swift tutorial, your next topic will be learning about: Swift readStructured.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.