0 Replies
      Latest reply: Sep 9, 2016 12:59 PM by WaterMonkey RSS
      WaterMonkey Level 1 Level 1 (0 points)

        I'm currently stuck translating the code below for my apps database in firebase:

        Swift :

         

        var databaseRef = FIRDatabase.database().reference() 
        var userDict = NSDictionary?() 
        var userNamesArray = [String]()
         var userImagesArray = [String]() 
        
        
        override func viewDidLoad() {
         super.viewDidLoad() 
        
        
        self.databaseRef.child(@"users").observeEventType(.Value, withBlock : {
         (snapshot in 
        
        
        self.userDict = snapshot.value as? NSDictionary
        
        
        
        for(userId,details) in self.userDict!{ 
        let img = details.objectForKey(@"profile_pic_small" as! String 
        let name = details.objectForKey(@"Name" as! String 
        let firstName = name.componentsSeperatedByString(" ")[0] 
        self.userImagesArray.append(img) 
        self.userNamesArray.append(firstName) 
        self.collectionView?.reloadData()
         } 
        })
        
        

         

        The code below is my attempt at translating but i'm definetly missing something:

         

        
        - (void)viewDidLoad {
            [super viewDidLoad];
            _userNamesArray = [NSMutableArray new];
            _userImagesArray = [NSMutableArray new];
        
            self.databaseRef = [[FIRDatabase database] reference];
        
        
            [[_databaseRef child:@"users"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
             
                _usersDict = snapshot.value;
             
                NSString *details;
                for (details in _usersDict) {
                    NSString *img = (NSString *)[details objectForKey:@"profile_pic_small"]; 
                    NSString *name =(NSString *)details objectForKey:@"Name"];
                    NSString *firstName; firstName = [[name componentsSeparatedByString:@" "] objectAtIndex:0];
        
                       [_userImagesArray addObject:img];
                    [_userNamesArray addObject:name];
                    [self.collectionView reloadData];
                 
                }
             
             
             
             
            }];
        }
        
        

         

        Any help would be appreciated.