Argument `#1' cannot convert `string' expression to type `char[]' - unity3d

The best overloaded method match for `string.Split(params char[])' has some invalid arguments
Argument `#1' cannot convert `string' expression to type `char[]'
I'm trying to make a Text Dialogue but this error prevents me from compiling. What's wrong?
public TextAsset textFile;
public string[] textLines;
// Use this for initialization
void Start() {
if (textFile != null)
{
textLines = (textFile.text.Split("\n"));
}
}

string.Split has a couple of different overloads (combinations of parameters it can take), but none of them take a single string parameter. "\n" is a string literal, so it's an invalid argument.
One of the overloads takes a params char[], meaning you can either pass an array of chars, or you can just pass a bunch of individual chars and it will make the array for you. So you can use test.Split('\n') because single quotes ' denote a char literal rather than a string literal.

Related

How to pass substring from Swift to C?

I have the variable with the value "7438754876*567".I have got this from string as substring.I want to pass this value to c function.
void getRechargePinFromDestination(char* rechargeNumber)
{
setRechargeValue(rechargeNumber);
}
As I have declared as char, swift takes it as a Int8.How can I pass this value.Anyone please help me.Thanks in advance.
If the C function does not mutate the passed string then you should declare the argument as a const char*:
void getRechargePinFromDestination(const char* rechargeNumber)
{
// ...
}
Now you can pass Swift substrings by converting them to a String first (which is automatically converted to a temporary C string):
getRechargePinFromDestination(String(subString))
or with
subString.withCString { getRechargePinFromDestination($0) }
In either case, the C function is called with a pointer to a temporary C string representation, which is only valid during the function call.

Unused positional argument skipped in String formatting (Swift)

I want to format a string, in Swift, with two potential arguments (using Format specifiers). The string to format may have a place for only the first argument, only the second argument, or both arguments. If I use the first or both arguments it works, but if I use only the second argument, it does not work. For instance:
let title = "M."
let name = "David"
let greetingFormat = "Hello %1$# %2$#"
print(String(format: greetingFormat, title, name))
// OUTPUT> Hello M. David
// OK
If I use only the first argument in the String to format:
let greetingFormat = "Hello %1$#"
print(String(format: greetingFormat, title, name))
// OUTPUT> Hello M.
// OK
But when using only the second argument
let greetingFormat = "Hello %2$#"
print(String(format: greetingFormat, title, name))
// OUTPUT> Hello M.
// NOT THE EXPECTED RESULT!
In the last case I expected "Hello David". Is it a bug? How can I obtain the intended result for the last case where only the second argument is used?
Remarks:
Please note that this problem occurs in the context of localization (i.e. the string to format comes from a Localizable.strings file), so I don’t have the possibility to remove unused argument directly.
The question does not relate to person’s name formatting. This is just taken as a example.
I answer my own question but all credit to #Martin R that provides the relevant information in comments.
It is not a bug, String(format:) does not support omitting positional parameters.
It is a known behavior since ObjectiveC, see: stackoverflow.com/a/2946880/1187415
If you only have String arguments you can use multiple String substitutions with String.replacingOccurrences(of:, with:) instead of String(format:).
More precision on the last solution. The following will work in the case that only one argument is used and also if both arguments are used in the greetingFormat String:
greetingFormat.replacingOccurrences(
of: "%1$#", with: title)
.replacingOccurrences(
of: "%2$#", with: name)
Of course, with String.replacingOccurrences(of:, with:) you can choose other identifiers for the substitution than %1$# and %2$#.

Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers?

Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers:
`0029-otherwise-illegal-scala-literal`
See Scala explanation in Need clarification on Scala literal identifiers (backticks)
You can find the spec at https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#8.2
Section 2.2.2 tells you
The PropertyName production from the ECMAScript grammar is reproduced
below:
  PropertyName:    LiteralPropertyName    ComputedPropertyName
  LiteralPropertyName:    IdentifierName    StringLiteral
   NumericLiteral
  ComputedPropertyName:    [ AssignmentExpression ]
A property name can be any identifier (including a reserved word), a
string literal, a numeric literal, or a computed property name. String
literals may be used to give properties names that are not valid
identifiers, such as names containing blanks. Numeric literal property
names are equivalent to string literal property names with the string
representation of the numeric literal, as defined in the ECMAScript
specification.
This includes string literals.
You can declare a property as a string literal:
class MyClass {
"return" = 1;
}
you can access it with square brackets
let myinstance = new MyClass()
let one = myinstance["return"]

How to read swift headers

When I cmd click the split function in Xcode, it takes me to the header file. This is what it reads
public func split(maxSplit: Int = default, allowEmptySlices: Bool = default, #noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.SubSequence]
How does the following implementation work with above declaration?
someString.characters.split { $0 == "." }
Let's break it down:
public func split(maxSplit: Int = default, allowEmptySlices: Bool = default, #noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.SubSequence]
maxSplit: The first parameter, maxSplit, allows you to specify the maximum number of pieces that the sequence will be split into. The default is Int.max.
allowEmptySlices: The second parameter, allowEmptySlices specifies whether or not two consecutive separators in the sequence should lead to an empty slice. The default is false. For example if you had a String, "A..B", and you split on the . character, you could end up with either two (["A", "B"]) or three (["A", "", "B"]) items in the output array depending on what you pass for this parameter.
isSeparator: The last parameter is the closure that you pass to identify where to split the sequence.
Since both maxSplit and allowEmptySlices have default arguments, you don't need to include them in your function call unless you want to change them. The only parameter that you have to supply is the isSeparator closure.
In your case, you called:
someString.characters.split { $0 == "."}
...which is the equivalent of:
someString.characters.split(maxSplit: Int.max, allowEmptySlices: false) { $0 == ".' }
You could also write your function call like this:
someString.characters.split(isSeparator: { $0 == "." })
The way that you have written it makes use of the "trailing closure" syntax. If a function takes a closure as it's final argument, you can move the closure outside the parentheses like this:
someString.characters.split() { $0 == "." }
And if the function takes only one argument (not counting any default arguments that you are not supplying) then you can omit the parentheses altogether:
someString.characters.split { $0 == "." }
At the highest level, what happens is that split iterates through the sequence of characters. It tests each character using the supplied closure, and if the closure returns true, it splits the sequence on that character. In your case it will split the sequence of characters every time it locates a ".".
Some other notes:
rethrows: The whole function is marked rethrows. It will throw an error, but only if the closure that you pass for the isSeparator argument throws an error itself. Note that the isSeparator parameter allows you to pass a closure that throws an error, but you don't have to. Any time a function accepts a closure that throws an error, it will also accept a closure that does not throw. This is because non-throwing functions are a sub-type of throwing functions.
#noescape: The isSeparator parameter is marked #noescape. That simply means that nothing in the closure will survive past the end of the call to split.
Your line someString.characters.split { $0 == "."} uses the default values for maxSplit and allowEmptySlices and specifies a custom closure for isSeparator. Some longer version of calling the function split is:
let arr = str.characters.split(Int.max, allowEmptySlices: false) { (char) -> Bool in
char == "."
}
Since the above code uses the same values as the actual defaults (Int.max and false) you can remove the first two parameters and since the closure is the last parameter (making it a trailing closure) you can omit the entire parameter and simply write a closure in the following way:
let arr = str.characters.split { (char) -> Bool in
char == "."
}
Now we can simplify the closure by omitting the specific signature:
let arr = str.characters.split { $0 == "." }
someString.characters.split { $0 == "."}
is equivalent to
someString.characters.split(isSeparator: { $0 == "."})
the closure in your code is called a trailing closure, closures that are the last parameter to a function can be placed after the function declaration, and if no other parameters remain, you can also omit ().
The first two parameters have default values, so they can be omitted, leading to the simplified call you posted.

Swift: Trying to print an empty string – The process has been returned to the state before expression evaluation

I have a model with a column entry, which is a String. Sometimes entry has some text in it, sometimes it doesn't and it's an empty string.
When I try to print the string in console, I'm getting The process has been returned to the state before expression evaluation. (running e object.entry)
Not sure why it isn't just printing ""
Trying e object.entry! gives me error: operand of postfix '!' should have optional type; type is 'String'
Any ideas on how to fix this?
Empty String "" doesn't mean nil, and entry is not an optional type.
I think you are supposed to declare entry as "" when initialize the object
class object {
var entry = ""
//do anything you want next
}