How to mount an array dynamically

Asked

Viewed 95 times

0

I’d like to make a func that returns me the following array, however I don’t know the return type or how to dynamically assemble this array:

vDataSource_Detalhe = NSArray(objects:
    NSArray(objects: "Cerveja 01","Cerveja 02"),
    NSArray(objects : "Refrigerante 01","Refrigerante 02","Refrigerante 03"),
    NSArray(objects : "Guarana 01","Guarana 02","Guarana 03","Gurana 04"),
    NSArray(objects : "Guaravita 01"),
    NSArray(objects : "Caipirinha 01","Caipirinha 02","Caipirinha 03"),
    NSArray(objects : "Coca-Cola 0'","Coca-Cola 02","Coca-Cola 03"),
    NSArray(objects : "Pepsi 01","Pepsi 02")
)

I wish the skeleton of the funct looked like this :

func retorna(_ pQtd_Linhas : Int)-> ???????  /* 01 - como deve ficar estas interrogaçoes*/
{
    let vResultado = ???????();  /*02 como deve ficar estas interrogaçoes*/
    var vLinha : ??????;  /*03 como deve ficar estas interrogaçoes*/
    for A in 0...pQtdLinhas - 1
    {
        vLinha = ?????; /*04 como deve ficar estas interrogaçoes*/
        let C = Gerar_Qtd_Randomica();
        for B in 1...C
        {
            vItem = ????();  /*05 como deve ficar estas interrogaçoes*/
            vItem.append(Gerar_nome_randomico());
            vLinha.append(vItem)  /* é append mesmo??*/

        }
        vResultado.append(vLinha); /* é append mesmo??*/
    }
    return vResultado;
}
  • It would be something like that: anArray += ["a", "b", "c"]
anArray.append(contentsOf: ["a", "b", "c"]) `, but see here

  • Here’s more over array.

1 answer

0

Well, I guess it depends. You can put in the return [Any], that will serve to array anything. BUT I don’t know if this is what you need.

From your code, it seems to me that you simply need a two-dimensional String array, doesn’t it? In this case, what you need to return is [[String]] and your vDataSource_Dental can be:

vDataSource_Detalhe = [["Cerveja 01","Cerveja 02"], 
     ["Refrigerante 01","Refrigerante 02","Refrigerante 03"],
     ["Guarana 01","Guarana 02","Guarana 03","Gurana 04"],
     ["Guaravita 01"],
     ["Caipirinha 01","Caipirinha 02","Caipirinha 03"],
     ["Coca-Cola 0'","Coca-Cola 02","Coca-Cola 03"],
     ["Pepsi 01","Pepsi 02"]]
  • Good night, Ricardo.

  • Good evening Andreza. 
Quero implementar um pickerview tipo ""Estado-> Cidade"" 
para usar na func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
e 
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? . For cities I’m having trouble. I was only able to make it work by assembling the STATICALLY array of the form as I demonstrated first. When you click on the RJ state I want to define the cities of it. With the Two-Dimensional Array as you said I couldn’t use it in both functions correctly.

  • In this case, as you will only carry the city after selecting the state, I understand that what you need are two arrays: one for state and one for cities. The status you already fill with all the states. The of the cities you will leave empty (not nil, but empty) until the user selects the state. When it selects, you fill this array and have pickerView do the Reload to use delegate functions (numberOfRows and titleForRow). Have you tried that?

  • Good morning Andreza. .Então.. When I assemble the first item of the STATUS array, I want to assemble the ARRAY vDataSource_Detalhe.. and this is my question... HOW TO MOUNT IT DYNAMICALLY in the structure I spent : vDataSource_Detalhe = Nsarray(Objects: Nsarray(Objects: "Beer 01","Beer 02"), etc.... So when the LEFT side (state) of the pickerview is selected RJ on the RIGHT side will be all the cities of it. The delegate functions I know how to use them. The problem really is to mount the array this way.. Statically it works... :)

  • What you mean by "dynamically" is simply updating the city array when selecting the state value?

  • The logic I imagined for the viewcontroler in question is: When enter consult only once the base and load all arrays. Dynamically..: The function I demonstrate at first DYNAMICALLY the array ... But I DON’T know the values that are in the question marks.

  • Ricardo, sorry, but I don’t understand this as a dynamic array. If the values are always fixed, the array is static. BUT, as I said in the reply, you can return [Any] that is good for anything. Have you tried this?

  • Good evening.: ) My question is simple... : Assemble a function that returns EXACTLY that type of array.. I can do functions by returning [Any]... [Classe_tal]... [[xxx]]... Array<yyyy>.. ETC.. I just want to know how to make a function return THAT INITIAL STRUCTURE. hugs.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.