Taxonomy

View as Markdown

Taxonomy

Taxonomy helps you categorize pieces of content within your stack to facilitate easy navigation and retrieval of information.

initWithStack

The initWithStack method initializes a new instance of the Taxonomy class using the specified Stack.

NameTypeDescription
stack (required)String

The stack instance for making API requests

Objective C:

Stack *stack = [Contentstack stackWithAPIKey:@"api_key" 

                             deliveryToken:@"delivery_token" 

                              environment:@"environment"];

Taxonomy *taxonomy = [[Taxonomy alloc] initWithStack:stack];

Swift:

let stack = Contentstack.stack(apiKey: "api_key",

                             deliveryToken: "delivery_token",

                             environment: "environment")

let taxonomy = Taxonomy(stack: stack)

query

The querymethod generates a new Query instance for the taxonomy.

Objective C:

Taxonomy *taxonomy = [[Taxonomy alloc] initWithStack:stack]; 

Query *query = [taxonomy query];

Swift:

let taxonomy = Taxonomy(stack: stack) 

let query = taxonomy.query()

whereKey:equalTo:

The whereKey:equalTo: method adds a condition to filter entries where the specified key matches the provided value.

NameTypeDescription
keyNSString*

The key of the field to query for the desired data.

valueid

The value to match

Objective C:

Taxonomy *taxonomy = [[Taxonomy alloc] initWithStack:stack]; 

Query *query = [taxonomy query]; 

[query whereKey:@"title" equalTo:@"Sample Taxonomy"];

Swift:

let taxonomy = Taxonomy(stack: stack) 

let query = taxonomy.query() 

query.whereKey("title", equalTo: "Sample Taxonomy")

fetch:completion:

The whereKey:equalTo: method adds a condition to filter entries where the specified key matches the provided value.

NameTypeDescription
paramsNSDictionary<NSString*, id>*

Optional parameters for the request

completionBlockBlock

Completion handler with result or error

Objective C:

NSDictionary *params = @{ @"include_count":@(true), @"include_global_field_schema": @(true) }; 

[taxonomy fetch:params completion:^(NSDictionary *entries, NSError *error) { 

if (error) 

     { NSLog(@"Error: %@", error.localizedDescription); 

 return; 

     }

 NSLog(@"Fetched entries: %@", entries); 

     }];

Swift:

let params = [ "include_count": true, "include_global_field_schema": true ] as [String : Any] 

taxonomy.fetch(params) { (entries, error) in 

if let error = error { 

print("Error: \(error.localizedDescription)") 

return } 

if let entries = entries {

print("Fetched entries: \(entries)") 

  } 

}

findTaxonomy:

The findTaxonomy: method executes a query to fetch taxonomy entries that match specified query conditions.

NameTypeDescription
completionBlockcompletion

Returns (ResponseType, QueryResult*, NSError*)

Objective C:

Query *query = [taxonomy query];

[query whereKey:@"taxonomies.one" equalTo:@"term_one"];

[query findTaxonomy:^(ResponseType type, QueryResult *result, NSError *error) {

if (error) {

   NSLog(@"Error: %@", error.localizedDescription); 

   return; 

 } 

 NSArray *entries = [result getResult];

 NSLog(@"Fetched entries: %@", entries);

}];

Swift:

let query = taxonomy.query()

query.whereKey("taxonomies.one", equalTo: "term_one")

query.findTaxonomy { (type, result, error) in

    if let error = error {

        print("Error: \(error.localizedDescription)")

        return

    }

    if let entries = result?.getResult() {

        print("Fetched entries: \(entries)")

    }

}

orWithSubqueries:

The orWithSubqueries method combines multiple queries using AND condition.

NameTypeDescription
subqueriesNSArray<Query*>*

Array of Query objects to combine with OR

Objective C:

Query *query1 = [taxonomy query]; 

[query1 whereKey:@"taxonomies.one" equalTo:@"term_one"]; 

Query *query2 =[taxonomy query]; 

[query2 whereKey:@"taxonomies.two" equalTo:@"term_two"]; 

Query *mainQuery = [taxonomy query]; 

[mainQuery orWithSubqueries:@[query1, query2]];

Swift:

let query1 = taxonomy.query() 

query1.whereKey("taxonomies.one", equalTo: "term_one") 

let query2 = taxonomy.query()

query2.whereKey("taxonomies.two", equalTo: "term_two")

let mainQuery = taxonomy.query()

mainQuery.orWithSubqueries([query1, query2])

andWithSubqueries:

The andWithSubqueries: method combines multiple queries using AND condition.

NameTypeDescription
subqueriesNSArray<Query*>*

Array of Query objects to combine with AND

Objective C:

Query *query1 = [taxonomy query]; 

[query1 whereKey:@"taxonomies.one" equalTo:@"term_one"]; 

Query *query2 =[taxonomy query]; 

[query2 whereKey:@"taxonomies.two" equalTo:@"term_two"]; 

Query *mainQuery = [taxonomy query]; 

[mainQuery andWithSubqueries:@[query1, query2]];

Swift:

let query1 = taxonomy.query() 

query1.whereKey("taxonomies.one", equalTo: "term_one") 

let query2 = taxonomy.query()

query2.whereKey("taxonomies.two", equalTo: "term_two")

let mainQuery = taxonomy.query()

mainQuery.andWithSubqueries([query1, query2])