Hi,

I have a couple of design documents that holds some views for example a 
document-types doc with theses views 

                        ...
                        if (typeof doc.type !== 'undefined') {
                            if (doc.type === 'foo') {
                                emit(doc.type, {
                                    title: doc.title,
                                    attribute: doc.attribute
                                });
                            }
                            else if (doc.type === 'bar') {
                                emit(doc.type, {
                                    title: doc.title,
                          ....



then I have a utility function that returns a compiled view (the current 
working solution in Java ~ CouchUtils.java) that can be queried.

In swift I have a small starter problem i roughly do

       let rev = database.existingDocumentWithID("_design/document-types")
       let views = rev.propertyForKey("views")

Now when I try to look up the properties of `views` then I keep getting:

    MyIOSAPP[26894:207053] -[__NSCFDictionary propertyForKey:]: 
unrecognized selector sent to instance 0x7fcc99e9a7e0

I also get this error when trying out:

    println(views.propertyList())
    println(views.propertyName())
    println(views.propertiesToSaveForDeletion())

 
Any hints appreciated !


Thx

Pelle

-- 
You received this message because you are subscribed to the Google Groups 
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mobile-couchbase/a00c499b-1b54-4ac3-91b3-ff7d7bb78413%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
package com.example.utils;


import android.util.Log;
import com.couchbase.lite.Mapper;
import com.couchbase.lite.Reducer;
import com.couchbase.lite.internal.RevisionInternal;
import dk.monsenso.couchbase.Database;
import dk.monsenso.couchbase.View;

import java.util.EnumSet;
import java.util.Map;

public class CouchUtil {
    private static final String TAG = "CouchUtil";

    public static View getView(Database database, String designDoc, String viewName) {
        RevisionInternal rev = database.getDocumentWithIDAndRev("_design/" + designDoc,
                null, EnumSet.noneOf(com.couchbase.lite.Database.TDContentOptions.class));

        if (rev == null) {
            return null;
        }

        Map<String,Object> views = (Map<String,Object>)rev.getProperties().get("views");
        Map<String,Object> viewProps = (Map<String,Object>)views.get(viewName);
        if(viewProps == null) {
            return null;
        }
        // If there is a CouchDB view, see if it can be compiled from source:
        String tdViewName = String.format("%s/%s", designDoc, viewName);
        return compileView(database, tdViewName, viewProps);
    }

    public static View compileView(Database db, String viewName, Map<String,Object> viewProps) {
        String language = (String)viewProps.get("language");
        if(language == null) {
            language = "javascript";
        }
        String mapSource = (String)viewProps.get("map");
        if(mapSource == null) {
            return null;
        }
        Mapper mapBlock = View.getCompiler().compileMap(mapSource, language);
        if(mapBlock == null) {
            Log.w(TAG, String.format("View %s has unknown map function: %s", viewName, mapSource));
            return null;
        }
        String reduceSource = (String)viewProps.get("reduce");
        Reducer reduceBlock = null;
        if(reduceSource != null) {
            reduceBlock = View.getCompiler().compileReduce(reduceSource, language);
            if(reduceBlock == null) {
                Log.w(TAG, String.format("View %s has unknown reduce function: %s", viewName, reduceBlock));
                return null;
            }
        }

        View view = db.getView(viewName);
        view.setMapReduce(mapBlock, reduceBlock, "1");
        String collation = (String)viewProps.get("collation");
        if("raw".equals(collation)) {
            view.setCollation(com.couchbase.lite.View.TDViewCollation.TDViewCollationRaw);
        }
        return view;
    }
}

Reply via email to