<div dir="ltr">I have this class I wrote which stores the error messages from the firebaseauth if a error occurs.<div>The problem is that the variable on first return is not set and on second return is.</div><div>variable _errorMsg is empty on first return of method registerUser</div><div><br></div><div>Why is it not storing the variable when it&#39;s initially triggered?</div><div><br></div><div><br></div><div>







<p class="gmail-p1"><span class="gmail-s1">//</span></p>
<p class="gmail-p1"><span class="gmail-s1">//  Authentication.swift</span></p>
<p class="gmail-p2"><span class="gmail-s1"></span><br></p>
<p class="gmail-p3"><span class="gmail-s2">import</span><span class="gmail-s1"> Firebase</span></p>
<p class="gmail-p3"><span class="gmail-s2">import</span><span class="gmail-s1"> FirebaseAuth</span></p>
<p class="gmail-p3"><span class="gmail-s2">import</span><span class="gmail-s1"> FBSDKCoreKit</span></p>
<p class="gmail-p3"><span class="gmail-s2">import</span><span class="gmail-s1"> FBSDKLoginKit</span></p>
<p class="gmail-p2"><span class="gmail-s1"></span><br></p>
<p class="gmail-p3"><span class="gmail-s2">class</span><span class="gmail-s1"> Authentication: </span><span class="gmail-s3">UIViewController</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p2"><span class="gmail-s1"></span><br></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">public</span><span class="gmail-s1"> </span><span class="gmail-s2">var</span><span class="gmail-s1"> _errorMsg: </span><span class="gmail-s3">String</span><span class="gmail-s1"> = </span><span class="gmail-s4">&quot;&quot;</span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">private</span><span class="gmail-s1"> </span><span class="gmail-s2">var</span><span class="gmail-s1"> createdUser: </span><span class="gmail-s3">Bool</span><span class="gmail-s1">!</span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">private</span><span class="gmail-s1"> </span><span class="gmail-s2">var</span><span class="gmail-s1"> _userSignedIn = </span><span class="gmail-s2">false</span></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p1"><span class="gmail-s5">   </span><span class="gmail-s1">/**</span></p>
<p class="gmail-p1"><span class="gmail-s1">    *  Registers a user using email/password method</span></p>
<p class="gmail-p1"><span class="gmail-s1">    */</span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">func</span><span class="gmail-s1"> registerUser(email : </span><span class="gmail-s3">String</span><span class="gmail-s1">, password: </span><span class="gmail-s3">String</span><span class="gmail-s1">) -&gt; </span><span class="gmail-s3">Bool</span><span class="gmail-s1">{</span></p>
<p class="gmail-p3"><span class="gmail-s1">        </span><span class="gmail-s6">createdUser</span><span class="gmail-s1"> = </span><span class="gmail-s2">false</span></p>
<p class="gmail-p3"><span class="gmail-s1">        </span><span class="gmail-s6">FIRAuth</span><span class="gmail-s1">.</span><span class="gmail-s7">auth</span><span class="gmail-s1">()?.</span><span class="gmail-s7">createUser</span><span class="gmail-s1">(withEmail: email, password: password, completion: {(user, error) </span><span class="gmail-s2">in</span></p>
<p class="gmail-p3"><span class="gmail-s1">            </span><span class="gmail-s2">if</span><span class="gmail-s1"> error != </span><span class="gmail-s2">nil</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p3"><span class="gmail-s1">               </span><span class="gmail-s2">if</span><span class="gmail-s1"> </span><span class="gmail-s2">let</span><span class="gmail-s1"> errCode = </span><span class="gmail-s6">FIRAuthErrorCode</span><span class="gmail-s1">(rawValue: (error?.</span><span class="gmail-s3">_code</span><span class="gmail-s1">)!) {</span></p>
<p class="gmail-p3"><span class="gmail-s1">                </span><span class="gmail-s2">switch</span><span class="gmail-s1"> errCode {</span></p>
<p class="gmail-p3"><span class="gmail-s1">                    </span><span class="gmail-s2">case</span><span class="gmail-s1"> FIRAuthErrorCode.</span><span class="gmail-s7">errorCodeInvalidEmail</span><span class="gmail-s1">:</span></p>
<p class="gmail-p3"><span class="gmail-s1">                        </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s6">_errorMsg</span><span class="gmail-s1"> = </span><span class="gmail-s4">&quot;Invalid Email&quot;</span></p>
<p class="gmail-p3"><span class="gmail-s1">                        </span><span class="gmail-s8">print</span><span class="gmail-s1">(</span><span class="gmail-s4">&quot;DEBUG: invalid email&quot;</span><span class="gmail-s1">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                    </span><span class="gmail-s2">case</span><span class="gmail-s1"> FIRAuthErrorCode.</span><span class="gmail-s7">errorCodeEmailAlreadyInUse</span><span class="gmail-s1">:</span></p>
<p class="gmail-p3"><span class="gmail-s1">                        </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s6">_errorMsg</span><span class="gmail-s1"> = </span><span class="gmail-s4">&quot;Email already in use&quot;</span></p>
<p class="gmail-p3"><span class="gmail-s1">                        </span><span class="gmail-s8">print</span><span class="gmail-s1">(</span><span class="gmail-s4">&quot;DEBUG: in use&quot;</span><span class="gmail-s1">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                    </span><span class="gmail-s2">case</span><span class="gmail-s1"> FIRAuthErrorCode.</span><span class="gmail-s7">errorCodeWeakPassword</span><span class="gmail-s1">:</span></p>
<p class="gmail-p3"><span class="gmail-s1">                        </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s6">_errorMsg</span><span class="gmail-s1"> = </span><span class="gmail-s4">&quot;Enter a stronger password&quot;</span></p>
<p class="gmail-p3"><span class="gmail-s1">                        </span><span class="gmail-s8">print</span><span class="gmail-s1">(</span><span class="gmail-s4">&quot;DEBUG: stronger password&quot;</span><span class="gmail-s1">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                    </span><span class="gmail-s2">default</span><span class="gmail-s1">:</span></p>
<p class="gmail-p4"><span class="gmail-s5">                        </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: Create User Error: &quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                        </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s6">_errorMsg</span><span class="gmail-s1"> = </span><span class="gmail-s4">&quot;Unknown Error &quot;</span></p>
<p class="gmail-p2"><span class="gmail-s1">                    </span></p>
<p class="gmail-p3"><span class="gmail-s1">                   }</span></p>
<p class="gmail-p2"><span class="gmail-s1">                    </span></p>
<p class="gmail-p3"><span class="gmail-s1">                }</span></p>
<p class="gmail-p4"><span class="gmail-s5">                </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: New user failed to create&quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p2"><span class="gmail-s1">                </span></p>
<p class="gmail-p3"><span class="gmail-s1">            }</span><span class="gmail-s2">else</span><span class="gmail-s1">{</span></p>
<p class="gmail-p4"><span class="gmail-s5">                </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: New user created &quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s6">createdUser</span><span class="gmail-s1"> = </span><span class="gmail-s2">true</span></p>
<p class="gmail-p3"><span class="gmail-s1">            }</span></p>
<p class="gmail-p3"><span class="gmail-s1">        })</span></p>
<p class="gmail-p2"><span class="gmail-s1">        </span></p>
<p class="gmail-p5"><span class="gmail-s5">        </span><span class="gmail-s2">return</span><span class="gmail-s5"> </span><span class="gmail-s2">self</span><span class="gmail-s5">.</span><span class="gmail-s1">createdUser</span></p>
<p class="gmail-p2"><span class="gmail-s1">        </span></p>
<p class="gmail-p2"><span class="gmail-s1">        </span></p>
<p class="gmail-p3"><span class="gmail-s1">    }</span></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p1"><span class="gmail-s5">   </span><span class="gmail-s1">/**</span></p>
<p class="gmail-p1"><span class="gmail-s1">    * Registers a user using facebook login method</span></p>
<p class="gmail-p1"><span class="gmail-s1">    */</span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">func</span><span class="gmail-s1"> facebookLogin() -&gt; </span><span class="gmail-s3">Bool</span><span class="gmail-s1">{</span></p>
<p class="gmail-p2"><span class="gmail-s1">        </span></p>
<p class="gmail-p3"><span class="gmail-s1">        </span><span class="gmail-s2">let</span><span class="gmail-s1"> facebookLogin = </span><span class="gmail-s6">FBSDKLoginManager</span><span class="gmail-s1">()</span></p>
<p class="gmail-p3"><span class="gmail-s1">        facebookLogin.</span><span class="gmail-s7">logIn</span><span class="gmail-s1">(withReadPermissions: [</span><span class="gmail-s4">&quot;email&quot;</span><span class="gmail-s1">], from: </span><span class="gmail-s2">self</span><span class="gmail-s1">){ (result, error) </span><span class="gmail-s2">in</span></p>
<p class="gmail-p3"><span class="gmail-s1">            </span><span class="gmail-s2">if</span><span class="gmail-s1"> error != </span><span class="gmail-s2">nil</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p4"><span class="gmail-s5">                </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: Unable to authenticate with Facebook </span><span class="gmail-s5">\</span><span class="gmail-s1">(</span><span class="gmail-s5">error</span><span class="gmail-s1">)&quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">            }</span><span class="gmail-s2">else</span><span class="gmail-s1"> </span><span class="gmail-s2">if</span><span class="gmail-s1"> result?.</span><span class="gmail-s6">isCancelled</span><span class="gmail-s1"> == </span><span class="gmail-s2">true</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p4"><span class="gmail-s5">                </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: User cancelled authenticate with Facebook </span><span class="gmail-s5">\</span><span class="gmail-s1">(</span><span class="gmail-s5">error</span><span class="gmail-s1">)&quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">            }</span><span class="gmail-s2">else</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p4"><span class="gmail-s5">                </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: User success&quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                </span><span class="gmail-s2">let</span><span class="gmail-s1"> credential = </span><span class="gmail-s6">FIRFacebookAuthProvider</span><span class="gmail-s1">.</span><span class="gmail-s7">credential</span><span class="gmail-s1">(withAccessToken: </span><span class="gmail-s6">FBSDKAccessToken</span><span class="gmail-s1">.</span><span class="gmail-s7">current</span><span class="gmail-s1">().</span><span class="gmail-s6">tokenString</span><span class="gmail-s1">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s7">firebaseAuth</span><span class="gmail-s1">(credential)</span></p>
<p class="gmail-p3"><span class="gmail-s1">            }</span></p>
<p class="gmail-p2"><span class="gmail-s1">            </span></p>
<p class="gmail-p3"><span class="gmail-s1">        }</span></p>
<p class="gmail-p5"><span class="gmail-s5">        </span><span class="gmail-s2">return</span><span class="gmail-s5"> </span><span class="gmail-s1">_userSignedIn</span></p>
<p class="gmail-p3"><span class="gmail-s1">    }</span></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p1"><span class="gmail-s5">   </span><span class="gmail-s1">/**</span></p>
<p class="gmail-p1"><span class="gmail-s1">    * Authentication through firebase </span></p>
<p class="gmail-p1"><span class="gmail-s1">    * @parameter credential</span></p>
<p class="gmail-p1"><span class="gmail-s1">    */</span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">func</span><span class="gmail-s1"> firebaseAuth(</span><span class="gmail-s2">_</span><span class="gmail-s1"> credential: </span><span class="gmail-s6">FIRAuthCredential</span><span class="gmail-s1">){</span></p>
<p class="gmail-p2"><span class="gmail-s1">        </span></p>
<p class="gmail-p3"><span class="gmail-s1">        </span><span class="gmail-s6">FIRAuth</span><span class="gmail-s1">.</span><span class="gmail-s7">auth</span><span class="gmail-s1">()?.</span><span class="gmail-s7">signIn</span><span class="gmail-s1">(with: credential, completion: {(user, error) </span><span class="gmail-s2">in</span></p>
<p class="gmail-p3"><span class="gmail-s1">            </span><span class="gmail-s2">if</span><span class="gmail-s1"> error != </span><span class="gmail-s2">nil</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p4"><span class="gmail-s5">                </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: Failed auth with Firebase </span><span class="gmail-s5">\</span><span class="gmail-s1">(</span><span class="gmail-s5">error</span><span class="gmail-s1">)&quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s6">_userSignedIn</span><span class="gmail-s1"> = </span><span class="gmail-s2">false</span></p>
<p class="gmail-p3"><span class="gmail-s1">            }</span><span class="gmail-s2">else</span><span class="gmail-s1">{</span></p>
<p class="gmail-p4"><span class="gmail-s5">                </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: Success auth with Firebase&quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">                </span><span class="gmail-s2">self</span><span class="gmail-s1">.</span><span class="gmail-s6">_userSignedIn</span><span class="gmail-s1"> = </span><span class="gmail-s2">true</span></p>
<p class="gmail-p3"><span class="gmail-s1">            }</span></p>
<p class="gmail-p3"><span class="gmail-s1">        })</span></p>
<p class="gmail-p3"><span class="gmail-s1">    }</span></p>
<p class="gmail-p2"><span class="gmail-s1"></span><br></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s9">/**</span></p>
<p class="gmail-p1"><span class="gmail-s1">     * Checks if the user is already signed in and sets the variable.</span></p>
<p class="gmail-p1"><span class="gmail-s1">     */</span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">func</span><span class="gmail-s1"> signedIn() -&gt; </span><span class="gmail-s3">Bool</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p2"><span class="gmail-s1">        </span></p>
<p class="gmail-p3"><span class="gmail-s1">        </span><span class="gmail-s2">if</span><span class="gmail-s1"> ((</span><span class="gmail-s6">FIRAuth</span><span class="gmail-s1">.</span><span class="gmail-s7">auth</span><span class="gmail-s1">()?.</span><span class="gmail-s6">currentUser</span><span class="gmail-s1">) != </span><span class="gmail-s2">nil</span><span class="gmail-s1">) {</span></p>
<p class="gmail-p4"><span class="gmail-s5">            </span><span class="gmail-s8">print</span><span class="gmail-s5">(</span><span class="gmail-s1">&quot;DEBUG: user is signed in&quot;</span><span class="gmail-s5">)</span></p>
<p class="gmail-p3"><span class="gmail-s1">            </span><span class="gmail-s6">_userSignedIn</span><span class="gmail-s1"> = </span><span class="gmail-s2">true</span></p>
<p class="gmail-p3"><span class="gmail-s1">        }</span></p>
<p class="gmail-p2"><span class="gmail-s1">        </span></p>
<p class="gmail-p5"><span class="gmail-s5">        </span><span class="gmail-s2">return</span><span class="gmail-s5"> </span><span class="gmail-s1">_userSignedIn</span></p>
<p class="gmail-p3"><span class="gmail-s1">    }</span></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p1"><span class="gmail-s5">   </span><span class="gmail-s1">/**</span></p>
<p class="gmail-p1"><span class="gmail-s1">    *  Stores the error messages for authentication issues</span></p>
<p class="gmail-p1"><span class="gmail-s1">    */</span></p>
<p class="gmail-p3"><span class="gmail-s1">    </span><span class="gmail-s2">var</span><span class="gmail-s1"> errorMsg: </span><span class="gmail-s3">String</span><span class="gmail-s1"> {</span></p>
<p class="gmail-p3"><span class="gmail-s1">        </span><span class="gmail-s2">get</span><span class="gmail-s1">{</span></p>
<p class="gmail-p3"><span class="gmail-s1">            </span><span class="gmail-s2">return</span><span class="gmail-s1"> </span><span class="gmail-s6">_errorMsg</span></p>
<p class="gmail-p3"><span class="gmail-s1">        }</span></p>
<p class="gmail-p3"><span class="gmail-s1">        </span><span class="gmail-s2">set</span><span class="gmail-s1">(value){</span></p>
<p class="gmail-p3"><span class="gmail-s1">            </span><span class="gmail-s6">_errorMsg</span><span class="gmail-s1"> = value</span></p>
<p class="gmail-p3"><span class="gmail-s1">        }</span></p>
<p class="gmail-p3"><span class="gmail-s1">    }</span></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p2"><span class="gmail-s1">    </span></p>
<p class="gmail-p3"><span class="gmail-s1">}</span></p></div></div>