PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] Why is my array is always nil, I'm try to add an item to the array in order to share a document

 May 13, 2022     append, arrays, swift, swiftui, xcode     No comments   

Issue

I'm try to share a PDF document which is display using Quick Look view.

I can correctly see the PDF loaded in the PDF view but I can't add the PDF path to the arrayItemToShare.

I have tried many things but can't find the reason why this code not adding anything!

arrayItemToShare?.append(report.pathFile!)

below the code of my report view.

struct listReport: View {
    @ObservedObject var dm : DataManager
    var report : UserReport
    
    @State var openQL = false
    @State var share = false
    
    @State var arrayItemToShare : [String]?
    var body: some View {

        Button(action: {
            
            openQL.toggle()
            
        }, label: {
            VStack(alignment: .center, spacing: 60){
                if report.urlFile != nil {
                    PDFThumbnailRepresented(urlDoc: report.urlFile!).padding()
                }
                Spacer()
                Text(report.id_report_show).foregroundColor(.primary).bold()
            }
        })
        .sheet(isPresented: $openQL, content: { // open sheet to preview PDF
            fakebar
            if report.pathFile != nil {
                QLView(filePath: report.pathFile!, isPresented: $openQL) // open Quick Look and preview the file pdf at path
                    .onAppear {
                    arrayItemToShare?.append(report.pathFile!) // why this not adding the value to the array
                    print("count \(arrayItemToShare!.count)") // here the crash because array is NIL
                }
            } else {
                Text("OPS!!.. unable to load pdf")
            }
        })
        .sheet(isPresented: $share) {
            ShareSheet(item: arrayItemToShare!)
        }
        
    }
    
    var fakebar: some View {
        ZStack {
            HStack {
                Button(action: {
                    if report.urlFile != nil {
                        openQL.toggle() // close preview pdf
                        
                        share.toggle() // open share sheet
                        
                    }
                }) {
                    Image(systemName: "square.and.arrow.up")
                        .foregroundColor(.white)
                        .padding(.horizontal)
                }

                Spacer()
                
                Image(systemName: "chevron.compact.down")
                    .font(.system(size: 60))
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(.white)
                
                Spacer()
                
                Button(action: {
                    openQL.toggle()
                }) {
                    Text("Close")
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                        .padding(.horizontal)
                }
            }
        }
        .frame(height: 44)
        .background(Color("AMUColor").padding(.top, -44))
    }
    
}

I use this (listReport: View) inside a scrollView with a foreach loop, where everything work fine.


Solution

Here is a fix:

QLView(filePath: report.pathFile!, isPresented: $openQL) // open Quick Look and preview the file pdf at path
    .onAppear {
    if nil == arrayItemToShare {
       arrayItemToShare = [String]()       // << create if needed !!
    }
    arrayItemToShare?.append(report.pathFile!)
    print("count \(arrayItemToShare!.count)")
}


Answered By - Asperi
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing