iOS 开发问题汇总(九)

1.Curried functions in Swift

A:There’s a difference between self.methodname (which you are using), and Classname.methodname.

The former, when called within a class’s method, will give you a function bound with that class instance. So if you call it, it will be called on that instance.

The latter gives you a curried function that takes as an argument any instance of Classname, and returns a new function that is bound to that instance. At this point, that function is like the first case (only you can bind it to any instance you like).

Here’s an example to try and show that a bit better:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class C {
    private let _msg: String
        init(msg: String) { _msg = msg }

    func c_print() { print(_msg) }

    func getPrinter() -> ()->() { return self.c_print }
}

let c = C(msg: "woo-hoo")
let f = c.getPrinter()
// f is of type (())->()
f() // prints "woo-hoo"

let d = C(msg: "way-hey")

let g = C.c_print
// g is of type (C) -> (()) -> (),
// you need to feed it a C:
g(c)() // prints "woo-hoo"
g(d)() // prints "way-hey"

// instead of calling immediately,
// you could store the return of g:
let h = g(c)
// at this point, f and h amount to the same thing:
// h is of type (())->()
h() // prints "woo-hoo"

Reference:Curried functions in SWIFT

2.NSLog on devices in iOS 10 / Xcode 8 will truncate.

A:A temporary solution, just redefine all NSLOG to printf in a global header file.

1
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

Reference:NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?

继续阅读

音频和视频格式

音频格式

我们通常说的音频格式准确地讲应该是音频文件格式,它是计算机系统上用于存放数字音频数据的文件格式,也可以看作一个容器。

音频数据的比特分布我们称为音频编码格式,它可以非压缩编码或压缩编码。压缩编码又分为无损压缩和有损压缩。

编码器(codec)就是来编解码原始音频数据的。

声音源 --ADC--> raw audio data --codec--> audio data(uncompressed/compressed) --packed--> audio file format(container format)

An audio file format is a file format for storing digital audio data on a computer system. The bit layout of the audio data (excluding metadata) is called the audio coding format and can be uncompressed, or compressed to reduce the file size, often using lossy compression. The data can be a raw bitstream in an audio coding format, but it is usually embedded in a container format or an audio data format with defined storage layer.

It is important to distinguish between the audio coding format, the container containing the raw audio data, and an audio codec. A codec performs the encoding and decoding of the raw audio data while this encoded data is (usually) stored in a container file. Although most audio file formats support only one type of audio coding data (created with an audio coder), a multimedia container format (as Matroska or AVI) may support multiple types of audio and video data.

There are three major groups of audio file formats:

• Uncompressed audio formats, such as WAV, AIFF, AU or raw header-less PCM;

• Formats with lossless compression, such as FLAC, Monkey’s Audio (filename extension .ape), WavPack (filename extension .wv), TTA, ATRAC Advanced Lossless, ALAC (filename extension .m4a), MPEG-4 SLS, MPEG-4 ALS, MPEG-4 DST, Windows Media Audio Lossless (WMA Lossless), and Shorten (SHN).

• Formats with lossy compression, such as Opus, MP3, Vorbis, Musepack, AAC, ATRAC and Windows Media Audio Lossy (WMA lossy).

继续阅读

Android开发问题汇总(二)

1.Error running app: Default Activity not found

A: 问题的原因是 AndroidManifest.xml 文件配置的有问题。正确的样例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.self.buildexample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".BuildExampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

2. How to rename java package in Android Studio?

A:Android Studio Rename Package

3.No service of type Factory available in ProjectScopeServices

A:Change maven gradle plugin version to 1.4.1 in project build.gradle file

1
2
3
4
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}

ReferenceNo service of type Factory available in ProjectScopeServices

4.Android Gradle cannot find symbol class Gson

A:In my case, I just added this line:

1
compile 'com.google.code.gson:gson:2.7'

on my app build.gradle file.

By now 2.7 is last current available version according to: https://mvnrepository.com/artifact/com.google.code.gson/gson

Please check this repository to be sure you are using last available version.

Reference:Android Gradle cannot find symbol class Gson

继续阅读