key-value

how can i get key's value from dictionary in Swift?

試著忘記壹切 提交于 2019-11-29 22:45:56
I am early bird in swift. I have a dictionary.I want to get my key's value.Object for key method is not worked for me.Can anybody help me please? This is my dictionary; var companies = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc", "AMZN" : "Amazon.com, Inc", "FB" : "Facebook Inc"] for (name) in companies.key { println(companies.objectForKey("AAPL")) } Prine With this method you see the key and the value. var companies = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc", "AMZN" : "Amazon.com, Inc", "FB" : "Facebook Inc"] for (key, value) in companies { print("\(key) -> \(value)") } Or if you only

iPhone - How to detect if a key exists in NSUserDefaults standardUserDefaults

孤者浪人 提交于 2019-11-29 21:03:42
Using NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; , I use calls like BOOL boolFromPrefs = [defaults boolForKey:@"theBoolKey"]; To get a saved BOOL value. If the key is not found, NO is returned (default behaviour of boolForKey). But... NO can be a saved setting. Same thing for intForKey So how can I test if a key exists before trying to get its value ? Matthias Bauch Do it the right way and register default values . NSDictionary *userDefaultsDefaults = @{ @"SomeKey": @NO, @"AnotherKey": @"FooBar", @"NumberKey": @0, }; [NSUserDefaults.standardUserDefaults registerDefaults

Go map of functions

落爺英雄遲暮 提交于 2019-11-29 20:35:33
I have Go program that has a function defined. I also have a map that should have a key for each function. How can I do that? I have tried this, but this doesn't work. func a(param string) { } m := map[string] func { 'a_func': a, } for key, value := range m { if key == 'a_func' { value(param) } } Are you trying to do something like this? I've revised the example to use varying types and numbers of function parameters. package main import "fmt" func f(p string) { fmt.Println("function f parameter:", p) } func g(p string, q int) { fmt.Println("function g parameters:", p, q) } func main() { m :=

A KeyValuePair in Java

佐手、 提交于 2019-11-29 19:38:12
I'm looking for a KeyValuePair class in Java. Since java.util heavily uses interfaces there is no concrete implementation provided, only the Map.Entry interface. Is there some canonical implementation I can import? It is one of those "plumbers programming" classes I hate to implement 100x times. Eyal Schneider The documentation AbstractMap.SimpleEntry is generic and can be useful. kreker Android programmers could use BasicNameValuePair Update: BasicNameValuePair is now deprecated (API 22). Use Pair instead. Example usage: Pair<Integer, String> simplePair = new Pair<>(42, "Second"); Integer

Can integer keys / values be stored in LevelDB?

旧时模样 提交于 2019-11-29 14:56:49
问题 I have searched for key value stores that support integer keys and integer values. LevelDB seems a good option, though I can't find any information on whether integer values/keys are supported 回答1: You can store pretty much anything in LevelDB. You provide opaque slices of data into LevelDB via the Slice structure. Here is an example: int intKey = 256; int intValue = 256*256; Slice key((char*)&intKey, sizeof(int)); Slice value((char*)&intValue, sizeof(int)); db->Put(leveldb::WriteOptions(),

Build insert query from array MySQL and PHP

依然范特西╮ 提交于 2019-11-29 14:49:21
I'm writing a small web service in PHP and I'm having a bit of trouble getting my head around the following scenario. My plan is to be able to send an array of data to a function, which would then build a query for MySQL to run. In the array I plan for the key to be the column name, and the value to be the value going in to the columns.. i.e. $myData = array('userName'=>'foo','passWord'=>'bar'); $myClass = new users(); $myClass->addUser($myData); Then, in my function I currently have: function addUser($usrData){ foreach($usrData as $col => $val){ // This is where I'm stuck.. } } Basically, I

python基础—— 字典dict

末鹿安然 提交于 2019-11-29 14:04:00
文章目录 一、创建字典 1.1、使用 {} 来创建字典 1.2、使用 dict() 函数来创建字典 1.3、将一个包含有双值子序列的序列转换为字典 二、字典的方法 2.1、len() 获取字典中键值对的个数 2.2、in、not in 检查字典中指定的键 三、字典的读取 3.1、d[key] 根据键来获取值 3.2、get(key[, default]) 该方法用来根据键来获取字典中的值 四、修改字典的值 4.1、d[key] = value 4.2、setdefault(key[, default]) 可以用来向字典中添加key-value 4.3、update([other]) 将其他的字典 添加到当前字典中 五、字典的删除 5.1、del d[key] 来删除字典中的 key-value 5.2、popitem() 删除一个键值对,删除的key-value返回 5.3、pop(key[, default]) 删除字典中的key-value,并将被删除的value返回 六、字典的其他方法 6.1、clear() 用来清空字典 6.2、copy() 对字典进行浅复制 七、遍历字典 7.1、keys() 返回字典的所有的key 7.2、values() 7.3、 items() 一、创建字典 1.1、使用 {} 来创建字典 创建了一个空字典 d = { } 创建一个保护有数据的字典

Get index of a key/value pair in a C# dictionary based on the value

随声附和 提交于 2019-11-29 10:39:20
问题 I would like to know if some property or method exists that gets the index of a specific value. I found that dictionaries have the Contains() method which returns true if the value passed in exists, so this method almost implements what I need. I know that I can loop through all the value pairs and check the condition, but I ask because maybe there's an optimized way of doing this. 回答1: There's no such concept of an "index" within a dictionary - it's fundamentally unordered. Of course when

Why can't I compare a KeyValuePair<TKey, TValue> with default

不羁岁月 提交于 2019-11-29 10:37:32
问题 In .Net 2.5 I can usually get an equality comparison (==) between a value and its type default if (myString == default(string)) However I get the following exception when I try to run an equality comparison on a default KeyValuePair and a KeyValuePair Code Sample (from a pre-extension method, proto-lambda static ListUtilities class :) ) public static TKey FirstKeyOrDefault<TKey, TValue>(Dictionary<TKey, TValue> lookups, Predicate<KeyValuePair<TKey, TValue>> predicate) { KeyValuePair<TKey,

Can't add keyValuePair directly to Dictionary

橙三吉。 提交于 2019-11-29 09:04:38
I wanted to add a KeyValuePair<T,U> to a Dictionary<T, U> and I couldn't. I have to pass the key and the value separately, which must mean the Add method has to create a new KeyValuePair object to insert, which can't be very efficient. I can't believe there isn't an Add(KeyValuePair<T, U>) overload on the Add method. Can anyone suggest a possible reason for this apparent oversight? Backup a minute...before going down the road of the oversight, you should establish whether creating a new KeyValuePair is really so inefficient. First off, the Dictionary class is not internally implemented as a