Struct in Ionic Framework [duplicate] - swift

I have a struct like so:
struct tTest{
char foo [1+1];
char bar [64];
};
In TypesScript I have
export interface tTest{
foo: string;
bar: string;
}
Is there a way to add [64] and [1+1] to the type?

As the comments say: js/ts don't support the char type and there's no way to declare array/string lengths.
You can enforce that using a setter though:
interface tTest {
foo: string;
}
class tTestImplementation implements tTest {
private _foo: string;
get foo(): string {
return this._foo;
}
set foo(value: string) {
this._foo = value;
while (this._foo.length < 64) {
this._foo += " ";
}
}
}
(code in playground)
You'll need to have an actual class as the interfaces lacks implementation and doesn't survive the compilation process.
I just added spaces to get to the exact length, but you can change that to fit your needs.

You can't force the length of an array in Typescript, as you can't in javascript.
Let's say we have a class tTest as following:
class tTest{
foo = new Array<string>(2);
};
As you can see, we have defined an array of string with length 2, with this syntax we can restrict the type of values we can put inside our array:
let t = new tTest();
console.log('lenght before initialization' + t.foo.length);
for(var i = 0; i < t.foo.length; i++){
console.log(t.foo[i]);
}
t.foo[0] = 'p';
t.foo[1] = 'q';
//t.foo[2] = 3; // you can't do this
t.foo[2] = '3'; // but you can do this
console.log('length after initialization' + t.foo.length);
for(var i = 0; i < t.foo.length; i++){
console.log(t.foo[i]);
}
In this manner we can't put a number value inside your array, but we can't limit the number of values you can put inside.
Playground

Related

How to automatically parse out the fields(Protobuf) used in the code?

Given the following trade.proto file:
message Trade {
message ProductInfo {
optional uint64 product_id = 1;
optional String product_name = 2;
repeated int other_field = 3;
}
optional string id = 1;
optional uint64 partition_time = 2;
optional ProductInfo product_info= 3;
}
Given the following codeļ¼š
def getOtherFieldSize(trade: Trade): Int = {
if (20221205 == trade.getPartitionTime) {
trade.getProductInfo.getOtherFieldList.size
} else {
0
}
}
How to get the fields of trade.proto used in the code? For example:
partition_time
product_info.other_field

Printing a class item of integer type in flutter

Is there a specific method to show integer value in flutter.
I have a code snippet similar to :
class with integer value:
class item{
item({
this.int_number
});
int int_number=1;
}
To print an int value in flutter, we usually use Text("$number")
but when trying similar method to print the class value :
Text("$item.int_number")
The number isn't printed correctly. but only data about $item
Is there a direct solution to this problem ?? to print int values that is a class item.
Thanks
use Text("${item.int_number}")
to print the values of instances like obj.value you need to wrap it in {}
thus you can use print("${obj.value}");
EDIT:
Tested this code on dartpad
void main() {
var item = Item(intNumber: 5);
print("${item.intNumber}");
print(item);
}
class Item {
Item({this.intNumber});
int intNumber = 1;
toString() => "Item (int_number = $intNumber)";
}
So you should be able to use
Text('${item.intNumber}')
or
Text('$item')
Use toString to converting int variable into String
For Example :
int intValue = 1;
I will use this in Text like this
Text("Value is:" + intValue.toString)
For more info try this link

VS Code extension how to edit in context?

Her is the class I use automatically capitalize true, false, ...
export class StUpdater {
private _lines: number;
private _strings: Array<string>;
constructor() {
this._lines = 0;
this._strings = ['true', 'false', 'exit', 'continue', 'return'];
}
Update(Cntx: boolean = false) {
let editor = window.activeTextEditor;
if (!editor || (editor.document.languageId != 'st')) {
window.showErrorMessage('No editor!')
return;
}
let doc = editor.document;
if (Cntx == false) {
if (this._lines >= doc.lineCount) {
this._lines = doc.lineCount;
return;
}
this._lines = doc.lineCount;
let AutoFormat = workspace.getConfiguration('st').get('autoFormat');
if (!AutoFormat) {
return;
}
}
let edit = new WorkspaceEdit();
for (let line = 0; line < doc.lineCount; line++) {
const element = doc.lineAt(line);
for (let i = 0; i < this._strings.length; i++) {
let str = this._strings[i];
let last_char = 0;
while (element.text.indexOf(str, last_char) >= 0) {
let char = element.text.indexOf(str, last_char);
last_char = char + str.length;
edit.replace(
doc.uri,
new Range(
new Position(line, char),
new Position(line, last_char)
),
str.toUpperCase()
);
}
}
}
return workspace.applyEdit(edit);
}
public dispose() {
}
}
This code works fine, but I do not want to replace it inside the string or comment. How do I do that? I cannot find preg version of replace and even if I do, in one line I do not know if it is comment or not if it is multiple line comment.
If I understand you correctly you want capitalize only certain elements (identifiers probably), but not words in comments or strings, correct? That requires to identify lexical elements in the text, which is a mapping of a range of letters to a lexical type. This is usually done by a lexer. It's not difficult to write one by hand which walks over the characters on top of your current processing and find those ranges that can be manipulated.

vscode intellisense fails in Typescript chainable method which return "instance or number", how to fix it?

Consider the following code:
class BaseType {
private _group: any = {};
private _a: number;
constructor() { }
group(g?: any): this | any {
if (!g) {
return this._group;
}
this._group = g;
return this;
}
another(a?: number): this | number {
if (Number(a) === +a) {
this._a = a;
return this;
}
return this._a;
}
}
class Another {
constructor() { }
checkChainable() {
const inst = new BaseType().group({ name: 'test' }).another(20); //The intellisense is not working here
}
}
the only why I could fix the Syntax error in the VSCode is change the return type to this | any
Is there any why I can solve the VSCode intellisense issue and compile time error?
This is caused by how union types work.
For another, the result type is either this or a number, so you can only use properties/methods on the result that are common between those two types. You'd have to do a cast or type check if you want to use properties specific to BaseType:
const x = new BaseType().another(10)
const y = typeof x === 'number' ? x : x.another(20)
You don't get an error in the group case because you are instead returning this | any which basically reduces to any, since any allows access to any properties or methods. However you won't get good intellisense for the same reason

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code=0x0). error while making data structure

I am practicing my array form of data structure with swift.
I made a class "student"
and there are functions like display() and delete()
However, the application is not working.
There is an error message that
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code=0x0).
I think this error is about "optional" problem.
Here is my code.
class student
{
var studentArray = [[String]?]()
var numberOfStudents : Int = 10;
func display()
{
for (var i = 0; i < numberOfStudents ; i++)
{
print("{");
for (var j = 0; j < 2; j++)
{
print(studentArray[i]![j] + " ");
}
print("}");
}
}
func delete( value : String)
{
var i = 0
for ( i = 0; i < numberOfStudents ; i++)
{
if (value == studentArray[i]![1])
{
break;
}
}
if (i == numberOfStudents - 1 )
{
print("not found");
}
else
{
for (var k = i; k < numberOfStudents - 1 ; k++)
{
studentArray[k]![1] = studentArray[k+1]![1];
studentArray[k]![0] = studentArray[k+1]![0];
}
numberOfStudents--;
}
}
}
var hello = student()
hello.studentArray = [["0","0ee"],["9","9ee", ]]
hello.display() // I have a error at this point
hello.studentArray
Could anyone explain what is about it for me?
There are several mistakes in your code. The actual error is caused by your numberOfStudents variable, which is hard coded to 10, even though the array only contains 2 elements. Use studentArray.count in your for loop, not 10. Then read the Swift manual. You should not be using optionals nor C-style for loops in this example.
Here's how I would do it...
class Student { // Capitalise your classes
// Unnecessary whitespace removed
var studentArray: [[String]] = [] // No need for optionals here
/*
var numberOfStudents : Int = 10; // var is useless & wrong, also no need for semi-colon
*/
func display() {
/* A Swift-ier way to do this is
for student in studentArray {
print("{")
for field in student {
print(field + " ")
}
print("}")
}
However, using indexing:
*/
for i in 0 ..< studentArray.count {
print("{")
for j in 0 ..< studentArray[i].count { // Don't *know* this will be 2
print(studentArray[i][j] + " ") // Don't need semi-colons unless you want to put multiple statements on the same line
}
print("}")
}
}
/* func delete() not used in question, so removed from answer */
}
var hello = Student()
hello.studentArray = [["0","0ee"], ["9","9ee", ]] // Note spurious (but not wrong) comma
hello.display()
hello.studentArray